Question

1 #include <string> 2 #include <iostream> 3 using std::string; 4 using std::cout; 5 using std::endl; 7...

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. ___

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
1 #include <string> 2 #include <iostream> 3 using std::string; 4 using std::cout; 5 using std::endl; 7...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass...

    Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass { public: void print() const; baseClass(string s = " ", int a = 0); //Postcondition: str = s; x = a; protected: int x; private: string str; }; class derivedClass: public baseClass { public: void print() const; derivedClass(string s = "", int a = 0, int b = 0); //Postcondition: str = s; x = a; y = b; private: int y; }; int...

  • #include <iostream> #include <string> using std::string; using std::cout; using std::endl; void testAnswer(string testname, int answer, int...

    #include <iostream> #include <string> using std::string; using std::cout; using std::endl; void testAnswer(string testname, int answer, int expected) { if (answer == expected) cout << "PASSED: " << testname << " expected and returned " << answer << "\n"; else cout << "FAILED: " << testname << " returned " << answer << " but expected " << expected << "\n"; } // Implement printArray here void printArray(int array[],int b) { for(int i = 0; i<b;i++) { std::cout<< array[i] << "...

  • solve it in c++ 10 note: please do not give me same answer like this 1....

    solve it in c++ 10 note: please do not give me same answer like this 1. Define a Pet class that stores the pet's name, age, and weight. Add appropriate constructors, accessor functions, and mutator functions. Also define a function named getLifespan that returns a string with the value "unknown lifespan." Next, define a Dog class that is derived from Pet. The Dog class should have a private member variable named breed that stores the breed of the dog. Add...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • #include <iostream> #include <cstddef> using std::cout; using std::endl; class Node { int value; public: Node* left;...

    #include <iostream> #include <cstddef> using std::cout; using std::endl; class Node { int value; public: Node* left; // left child Node* right; // right child Node* p; // parent Node(int data) { value = data; left = NULL; right = NULL; p = NULL; } ~Node() { } int d() { return value; } void print() { std::cout << value << std::endl; } }; int main(int argc, const char * argv[]) { } function insert(Node *insert_node, Node *tree_root){ //Your code here...

  • #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,...

    #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,    double stArea, int yAdm, int oAdm) {    stateName = sName; stateCapital = sCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } void stateData::getStateInfo(string& sName, string& sCapital,    double& stArea, int& yAdm, int& oAdm) {    sName = stateName; sCapital = stateCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } string stateData::getStateName() { return stateName;...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • rewrite this c code in python #include <iostream> using std::cout; using std::cin; using std::endl; int charClass;...

    rewrite this c code in python #include <iostream> using std::cout; using std::cin; using std::endl; int charClass; char lexeme[100]; char str[200]; char nextChar; const int LETTER = 0; const int DIGIT = 1; const int UNKNOWN = -1; const int OPAREN = 2; const int CPAREN = 3; const int PLUS = 4; const int MINUS = 5; const int MUL = 6; const int DIV = 7; const int ID_CODE = 100; const int PLUS_CODE = 101; const int MINUS_CODE...

  • Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string;...

    Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string; class Account { public: Account(); Account(string, double); void deposit(double); bool withdraw(double); string getName() const; double getBalance() const; private: string name; double balance; }; #endif ////////////////////////////////////////////// //AccountManager.hpp #ifndef _ACCOUNT_MANAGER_HPP_ #define _ACCOUNT_MANAGER_HPP_ #include "Account.hpp" #include <string> using std::string; class AccountManager { public: AccountManager(); AccountManager(const AccountManager&); //copy constructor void open(string); void close(string); void depositByName(string,double); bool withdrawByName(string,double); double getBalanceByName(string); Account getAccountByName(string); void openSuperVipAccount(Account&); void closeSuperVipAccount(); bool getBalanceOfSuperVipAccount(double&) const;...

  • Answer this in c++ #include <iostream> #include <fstream> #include <string> using namespace std; class Person {...

    Answer this in c++ #include <iostream> #include <fstream> #include <string> using namespace std; class Person { public: Person() { setData("unknown-first", "unknown-last"); } Person(string first, string last) { setData(first, last); } void setData(string first, string last) { firstName = first; lastName = last; } void printData() const { cout << "\nName: " << firstName << " " << lastName << endl; } private: string firstName; string lastName; }; class Musician : public Person { public: Musician() { // TODO: set this...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT