Question

// Inheriting member functions // Please Make all changes as noted below: // 1) Make sayHello...

 //  Inheriting member functions
 // Please Make all changes as noted below:
 // 1) Make sayHello virtual function (hint this will be done in Person class
 // 2) Change main to create two Person pointer variables
 // 3) Set each pointer to one of the two objects already instantiated below
 // 4) Use the pointer variable to invoke the two sayHello() methods

 #include <iostream > 
 #include <string > 
 using namespace std ; 
  
 class Person { 
   protected : 
     string name; 
     int age ; 
   public : 
    Person () : name("" ) , age (0) 
     {cout << "New person created . " << endl ;} 
    Person ( string n , int a) 
     :name(n) , age(a) 
     {cout << "New person created . " << endl ;} 
  
    virtual void sayHello () { 
     cout << "Hello I am " << name << " . " ; 
     cout << "I am a person . " << endl ; 
    } 
    ~Person (){ cout << "Person deleted . " << endl ;} 
}; 
  
 class Student : public Person { 
   private : 
    double gpa ; 
   public :
    Student () : Person () , gpa (0.0) 
     {cout << "New student created . " << endl ;} 
    Student ( string n, int a , double g) 
     : Person (n , a) , gpa(g) 
      {cout << "New student created . " << endl ;} 
    ~Student (){ cout << "Student deleted . " << endl ;} 
    
        void sayHello () { 
         cout << "Hello I am " << name << " . " ; 
         cout << "I am a student . " << endl ; 
        }

 }; 
  
 int main () { 
  Person *p1, *p2;
  Person per1 ("John" , 22); 
  Student st1 ("Bob" , 20, 3.55); 
  
  p1 = &per1;
  p2 = &st1;
  
  p1->sayHello();   // same as (*p1).sayHello()
  p2->sayHello ();  // same as (*p2).sayHello()
  return 0; 
 }

Questions are on the top of code i.e. 1), 2), 3) and 4) to make changes in give code
0 0
Add a comment Improve this question Transcribed image text
Answer #1

All 4 are done

1) Person class has virtual method sayHello
2) Two pointer variable Person *p1 and *p2
3) They are initialised to Already created person and student object
4) They call sayHello() method.


----------------------------------------------------------------------------------------------------------------------------------

#include <iostream>

#include <string>

using namespace std ;

class Person {

protected :

string name;

int age ;

public :

Person () : name("" ) , age (0)

{cout << "New person created . " << endl ;}

Person ( string n , int a)

:name(n) , age(a)

{cout << "New person created . " << endl ;}

virtual void sayHello () {

cout << "Hello I am " << name << " . " ;

cout << "I am a person . " << endl ;

}

~Person (){ cout << "Person deleted . " << endl ;}

};

class Student : public Person {

private :

double gpa ;

public :

Student () : Person () , gpa (0.0)

{cout << "New student created . " << endl ;}

Student ( string n, int a , double g)

: Person (n , a) , gpa(g)

{cout << "New student created . " << endl ;}

~Student (){ cout << "Student deleted . " << endl ;}

void sayHello () {

cout << "Hello I am " << name << " . " ;

cout << "I am a student . " << endl ;

}

};

int main () {

Person *p1, *p2;

Person per1 ("John" , 22);

Student st1 ("Bob" , 20, 3.55);

p1 = &per1;

p2 = &st1;

p1->sayHello(); // same as (*p1).sayHello()

p2->sayHello (); // same as (*p2).sayHello()

return 0;

}

-----------------------------------------------------------------------------------------------------------------
SEE OUTPUT

PLEASE COMMENT if there is any concern.

==============================

Add a comment
Know the answer?
Add Answer to:
// Inheriting member functions // Please Make all changes as noted below: // 1) Make sayHello...
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
  • Use main.cpp, Student.h, Student.cpp (written below) and write classes StudentClub and a non-member function find youngest...

    Use main.cpp, Student.h, Student.cpp (written below) and write classes StudentClub and a non-member function find youngest to make main.cpp compile. Put the declaration and definition of find youngest in StudentClub.h and StudentClub.cpp separately. You may not modify provided files and only submit StudentClub.h and StudentClub.cpp. Non-member function find youngest is declared as follows. It returns the names of students who have the youngest age. Note there may exist more than one students who are youngest. std::vector find_youngest(const std::vector member); StudentClub...

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

  • TOE B.2 - 30 pts class Student public Student(0 cout <<"Student0, constructor called." << endl; exp...

    TOE B.2 - 30 pts class Student public Student(0 cout <<"Student0, constructor called." << endl; explicit Student string stuName): stuName(stuName) cout << "Student stuName), constructor called." << endl; -Student) cout<-Student0, destructor called." << endl; string getName) return this->stuName void sayHi0 cout<< "Student: Hi!" << endl 4 virtual void sayHello0 cout <<"Student: Hello!" << endl private string stuName "Student" 5 class CSStudent: public Student f public CSStudent0 0 explicit CSStudent string stuName) -CSStudent) Student) 0 6 cout<<"-CSStudent), destructor called."<<endl; void sayHello)...

  • Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design...

    Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design a Ship class that has the following members: - A member variable for the name of the ship (a string) - A member variable for the year that the ship was built (a string) - A contsructor and appropriate accessors and mutators - A virtual print function that displays the ship's name and the year it was built (nobody seems to get this part...

  • Please provide a multi lined pseudo code for the program below.....also it is giving me a...

    Please provide a multi lined pseudo code for the program below.....also it is giving me a build error? Could you possibly tell me how to fix the error too? #include <iostream> #include <string> using namespace std; class ship { public: string name; int year; ship(string n, int y) { name = n; year = y; } int getYear() { return year; } string getName() { return name; } void setYear(int y) { year = y; } void setName(string n) {...

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

  • This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to...

    This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to create a Car class and Police Officer class, to create a new simulation. Write a simulation program (refer to the Bank Teller example listed below) that simulates cars entering a parking lot, paying for parking, and leaving the parking lot. The officer will randomly appear to survey the cars in the lot to ensure that no cars are parked...

  • Can anyone help me to complete this C plus plus program? Please notice: You are required...

    Can anyone help me to complete this C plus plus program? Please notice: You are required to implement insertion sort by using smart pointer. Please notice: You are required to implement insertion sort by using smart pointer. Please notice: You are required to implement insertion sort by using smart pointer. Please notice: You are required to implement insertion sort by using smart pointer. Please notice: You are required to implement insertion sort by using smart pointer. //Implment insertion sort using...

  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

  • Deliverables app.java, myJFrame.java, myJPanel.java and student.java as requested below. (Use the student.java class that you already...

    Deliverables app.java, myJFrame.java, myJPanel.java and student.java as requested below. (Use the student.java class that you already have from previous labs) Contents Based on the graphics you learned this week Create an instance (an object, i.e., st1) of student in myJPanel Display his/her basic information Display for 10 times what he/she is up to (using the method whatIsUp() that your student class already has) -------------------------------------------------------------------------------------------------- \\app.java public class app { public static void main(String args[]) { myJFrame mjf = new myJFrame();...

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
ADVERTISEMENT