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