1 #include <string> 2 #include <iostream> 3 using std::string; 4 using std::cout
ID: 3764500 • Letter: 1
Question
1 #include <string>
2 #include <iostream>
3 using std::string;
4 using std::cout;
5 using std::endl;
7 class Pet
8 {
9 public:
10 string name;
11 virtual void print( ) const;
12};
13
14 class Dog:public Pet
15 {
16 public:
17 string breed;
18 virtual void print( ) const;
19};
20
21 int main( )
22 {
23 Dog vdog;
24 Pet vpet;
25 vdog.name = "Tiny";
26 vdog.breed = "Great Dane";
27 vpet = vdog;
28 vpet.print( );
29
30 Pet *ppet;
31 ppet = new Pet;
32 Dog *pdog;
33 pdog = new Dog;
34 pdog->name = "Tiny";
35 pdog->breed = "Great Dane";
36 ppet = pdog;
37 ppet->print();
38 pdog->print();
39
40 return 0;
41 }
42
43 void Dog::print( )const
44 {
46 cout << "breed: "<< breed <<endl;
47 }
48
49 void Pet::print() const
50 {
51 cout << "name: " << name << endl;
52 }
ANSWER THESE QUESTIONS
What is the base class called? ___________________________________
What is the derived class called? _________________________________
Are lines 11 and 18 a pure virtual function? How can you tell? __________
____________________________________________________________
What does lines 23-28 demonstrate? _______________________________
_____________________________________________________________
What does lines 30-38 demonstrate? _______________________________
_____________________________________________________________
Which example (lines 23-28 or line 30-38) is better to use? Why? _________
______________________________________________________________
What is the -> used for? ___________________________________________
Define virtual function. ___________________________________________
_______________________________________________________________
What does line 14 tell you? _________________________________________
T/ F If a derived class is virtual, then the base class is automatically virtual. ___
Explanation / Answer
1) Class Pet is a Based Class.
2) Class Dog is Derived Class because it is child class which inherited from parent based class Pet
3)yes line number 11 and 18 are pure virtual function because
virtual void print() const; declared in Base Class and Also in child class
4) line no 23 to 28
Class Dog Creates the Object named as vdog
Class Pet Creates the Object named as vpet
vdog.name="Tiny" Here object name vdog access the name variable with value Tiny
vdog.breed="Great Dane"... Here object name vdog access the name variable breed with value Great Dane
vpet=vdog Here object vdog assign to vpet and After that call vpet object
5) Pet * ppet; // declare pointer object
ppet=new Pet; // creates Object ppet
Dog *pdog ; // declare pointer object with name *pdog
pdog=new Dog; // creates Object pdog
6) lines 23 to 28 is easy to used beacuase you directly used object with variable name
7) -> that point value to objects reference
8) virtual function ===
virtual void print()
Virtual function is a member function that u expect to redefined in derived class so when you refer derived class object using point or reference to the base class you can call virtual funcion for the object
9)
line 14 ::class Dog:public Pet
Class Dog It Inherits the class Pet so Class Dog access the method and properties of Pet class.
10) yes that becomes virtual
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.