Question

Question 1: A Daughter and a Son class inherit the Mother class. Each of the classes...

Question 1: A Daughter and a Son class inherit the Mother class. Each of the classes has print() which outputs the name as class member variable. In the following user code

void callprint(Mother *p){

p->print();

}

int main(){

Daughter *d;

d = new Daughter (“Jessica”);

Mother * m;

m= new Mother (“Jasmine”);

callPrint(m);

Son *s;

s = new Son (“Jeffrey”);

m = d;

callPrint(m);

s = m;

callPrint(m);

system(“pause’);

return 0;

}

  1. What are the multiple flaws in the code? Cite them(Marks 7)
  2. Rectify the code and use polymorphism of three Mother pointer variables to output the Mother, Daughter and Son names using callPrint()(Marks 8)





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

a)the multiple flaws in the code:

The flaws in the program are
1) callprint() must be written instead of callPrint()
2) Can't convert from Mother* to Son* in s=m statement since base class reference can't be converted to child class reference

b) use polymorphism of three Mother pointer variables to output the Mother, Daughter and Son names using callPrint():

Rectified user code is given below

void callprint(Mother *p){
p->print();
}
int main(){
Daughter *d;
d = new Daughter("Jessica");
Mother * m;
m= new Mother ("Jasmine");
callprint(m);
Son *s;
s = new Son ("Jeffrey");
m = d;
callprint(m);
m=s;
callprint(m);
return 0;
}

I am also giving the code for reference:

#include<iostream>
#include<string>
using namespace std;
class Mother
{
public:
string data;
Mother(){}
Mother(string data)
{
this->data=data;
}
void print()
{
cout<<data;
}
};
class Daughter:public Mother
{
public:
Daughter(string data)
{
this->data=data;
}
void print()
{
cout<<data;
}
};
class Son:public Mother
{
public:
Son(string data)
{
this->data=data;
}
void print()
{
cout<<data;
}
};

void callprint(Mother *p){
p->print();
}
int main(){
Daughter *d;
d = new Daughter("Jessica");
Mother * m;
m= new Mother ("Jasmine");
callprint(m);
Son *s;
s = new Son ("Jeffrey");
m = d;
callprint(m);
m=s;
callprint(m);
return 0;
}

Output

hp * 1.) 2:34 AM 1 koushikhp koushikhp@koushikhpnew:- koushikhp@koushikhpnew:$ g++ inher.cpp koushikhp@koushikhpnew :-$ ./a.o

Add a comment
Know the answer?
Add Answer to:
Question 1: A Daughter and a Son class inherit the Mother class. Each of the classes...
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
  • This java assignment will give you practice with classes, methods, and arrays. Part 1: Player Class...

    This java assignment will give you practice with classes, methods, and arrays. Part 1: Player Class Write a class named Player that stores a player’s name and the player’s high score. A player is described by:  player’s name  player’s high score In your class, include:  instance data variables  two constructors  getters and setters  include appropriate value checks when applicable  a toString method Part 2: PlayersList Class Write a class that manages a list...

  • Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class...

    Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class header instance variable UML class diagram encapsulation client visibility (or access) modifier accessor method mutator method calling method method declaration method invocation return statement parameters constructor Goals By the end of this activity you should be able to do the following: > Create a class with methods that accept parameters and return a value Understand the constructor and the toString method of a class...

  • Question 1 2 pts The preprocessor executes after the compiler. False True Question 2 2 pts...

    Question 1 2 pts The preprocessor executes after the compiler. False True Question 2 2 pts What code is human-readable and follows the standards of a programming language? Secret code Source code Key code None of these Machine code Question 3 2 pts What is the symbol that marks the beginning of a one line comment? Question 1 2 pts The preprocessor executes after the compiler. True False Question 5 2 pts A statement that may be used to stop...

  • I need this in C++. This is all one question. Introduction Your eighth assignment will consist...

    I need this in C++. This is all one question. Introduction Your eighth assignment will consist of two programs, which will involve the use of simple classes. The source code for these problems should be submitted using the naming conventions we specified in class. Please note that your computer programs should comply with the commenting and formatting rules as described in class. For example, there should be a header for the whole program that gives the author's name, class name,...

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

  • Assignment Overview In Part 1 of this assignment, you will write a main program and several...

    Assignment Overview In Part 1 of this assignment, you will write a main program and several classes to create and print a small database of baseball player data. The assignment has been split into two parts to encourage you to code your program in an incremental fashion, a technique that will be increasingly important as the semester goes on. Purpose This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of...

  • HELLO, PLEASE TAKE TIME TO ANSWER THIS QUESTION. PLESE ENSURE ITS CORRECT AND SHOW THAT THE...

    HELLO, PLEASE TAKE TIME TO ANSWER THIS QUESTION. PLESE ENSURE ITS CORRECT AND SHOW THAT THE PROGRAM RUNS. Task 1: Enforcing const-ness throughout Your first job will be to go through all of the code and decide which functions should be declared const. You should find several places throughout the program where this makes sense. We will also make the id data member in the Customer class const , as once a customer has been created their ID will never...

  • Last picture is the tester program! In this Assignment, you will create a Student class and...

    Last picture is the tester program! In this Assignment, you will create a Student class and a Faculty class, and assign them as subclasses of the superclass UHPerson. The details of these classes are in the UML diagram below: UHPerson - name : String - id : int + setName(String) : void + getName(): String + setID(int) : void + getID(): int + toString(): String Faculty Student - facultyList : ArrayList<Faculty - rank: String -office Hours : String - studentList...

  • ------------------------------------------------------------------------------------------------------------ CODE ALREADY HAVE BELOW--------------------------------------------------------------------------------------- public class LinkedQueue<T>

    ------------------------------------------------------------------------------------------------------------ CODE ALREADY HAVE BELOW--------------------------------------------------------------------------------------- public class LinkedQueue<T> implements QueueADT<T> {    private int count;    private LinearNode<T> head;    private LinearNode<T> tail;               public LinkedQueue()    {        count = 0;        head = null;        tail = null;    }    @Override    public void enqueue(T element)    {        LinearNode<T> node = new LinearNode<T> (element);        if(isEmpty())            head = node;        else           ...

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