上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VII

Recitation Class vil
Recitation Class VII

Content Objects and Classes
Content n Objects and Classes

Brief review ■Declare a class class ClassName { private: /data members /function members public: /function members ;
Brief Review n Declare a class class ClassName { private: // data members // function members public: // function members };

Example class DiamondT:public GObjectT double side: /size for the side length of the diamond double cx,cy:/center location of the diamond public: DiamondT () DiamondT (const double,const double,const double,string clr =Blue",const double density =1.0): void setSide (double length)[side =length: double getSide O freturn side:} void setLocation (const double x,const double y)fex=x:cy =y: double getX O freturn cx:} double getY O freturn cy: void draw O; //draw the diamond at the current location and color yoid正aw (const string elr,const double density =1.0); }:
Example

Objects and Classes ■Exercise1 The keyword is required to declare a class. A)class B)private C)public D)All of the above
Objects and Classes n Exercise 1 The keyword ________ is required to declare a class. A) class B) private C) public D) All of the above

Objects and Classes ■Exercise2 is invoked to create an object A)A function with a return type B)A constructor C)The main function D)A function with the void return type
n Exercise 2 ________ is invoked to create an object. A) A function with a return type B) A constructor C) The main function D) A function with the void return type Objects and Classes

Objects and Classes Exercise 3 #include int main( using namespace std; { Aa; class A a.print(; { public: int s; A(int newS) A)The program compiles and runs fine and prints nothing. B)The program has a compilation error because s=newS; class A is not a public class. } C)The program has a compilation error because class A does not have a default constructor. void print() D)The program would compile and run if you change Aa to Aa(5). cout <s:
n Exercise 3 #include using namespace std; class A { public: int s; A(int newS) { s = newS; } void print() { cout << s; } }; Objects and Classes int main() { A a; a.print(); } A) The program compiles and runs fine and prints nothing. B) The program has a compilation error because class A is not a public class. C) The program has a compilation error because class A does not have a default constructor. D) The program would compile and run if you change A a to A a(5)

Objects and Classes Exercise 4 int main() { #include Test test; using namespace std; cout <test.x; class Test { public: A)The program has a syntax error int x; because x has not been initialized. B)The program has a syntax error Test() because Test does not have a default constructor. cout <"Test"; C)The program has a syntax error because test is not initialized. D)The program runs fine,but test.x is unpredictable
n Exercise 4 #include using namespace std; class Test { public: int x; Test() { cout << "Test"; } }; int main() { Test test; cout << test.x; } A) The program has a syntax error because x has not been initialized. B) The program has a syntax error because Test does not have a default constructor. C) The program has a syntax error because test is not initialized. D) The program runs fine, but test.x is unpredictable. Objects and Classes

Objects and Classes Exercise 5 int main( #include using namespace std; Aa; cout<<a.X<""<<a.y<<""< class A a.z, { public: return 0; int x; int y; int z; A0:x(1),y(2),z(3) A)Error B)123
n Exercise 5 #include using namespace std; class A { public: int x; int y; int z; A(): x(1), y(2), z(3) { } }; int main() { A a; cout << a.x << " " << a.y << " " << a.z; return 0; } A) Error B) 1 2 3 Objects and Classes

Objects and Classes Exercise 6 #include using namespace std; int main( class Foo Foo foo; { foo.p(); public: int x;/data field return 0; int y;/data field Foo() { A)x is 10y is 10 X=10; 10; ◆B)×is20yis10 C)×is20yis20 D)×is10yis20 void p() { int x 20;/local variable cout<<"x is"<<<<"" eout <"yis "<<y<<endl;
n Exercise 6 #include using namespace std; class Foo { public: int x; // data field int y; // data field Foo() { x = 10; y = 10; } void p() { int x = 20; // local variable cout << "x is " << x << " "; cout << "y is " << y << endl; } }; int main() { Foo foo; foo.p(); return 0; A) x is 10 y is 10 B) x is 20 y is 10 C) x is 20 y is 20 D) x is 10 y is 20 Objects and Classes
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation V.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation IX.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_recitation 13.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation 1.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_vector_string.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Start with C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Random Number_Graphics.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_programming style guide for C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_objects and classes.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Vg101.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Computer and Programming.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Function.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_files_DataBase Design.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Expressions and Statements.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_examples on class design.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Array and its Applications.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_20 Looking Ahead.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_19 Recursion 1.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_16 MATLAB environment short.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_15 Introduction to matlab.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VIII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation X.ppt
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十四章 MCS-51单片机(1/2).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十四章 MCS-51单片机(2/2).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第一章 微机原理与接口技术绪论(朱兰娟).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第二章 8086系统结构.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter9 SQL in a server environment.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Pointer Review Solution.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Pointer Review.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Practice Final 1.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_practice Final 2.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_practice Final 3.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Solution for Practice Final 1.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Solution for Practice Final 2.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_I.运算符重载.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_II.继承与派生.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_III.多态性.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_IV.异常处理与模板.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_V. 流库.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_VI.类与对象.pdf