C++ Derived Classes Given the Person class: #include <iostream> #include <string
ID: 659759 • Letter: C
Question
C++ Derived Classes
Given the Person class:
#include <iostream>
#include <string>
using namespace std;
class Person{
private:
string name;
double age;
public:
Person() {
name = "NoName";
age = 0;
}
Person(string newname, int newage) {
name = newname;
age = newage;
}
void setName(string newname) {
name = newname;
}
void setAge(int newage) {
age = newage;
}
virtual void display(ostream& out){
out << "Name " << name << " Age " << age << endl ;
}
};
And the class Girl:
class Girl: public Person{
private:
int weight;
public:
Girl( ):Person("Heidi", 15){
weight = 100;
}
Girl(string newname, int newage, int newweight ){
weight = newweight;
}
void display(ostream& out) {
cout << "weight is " << weight << endl;
}
};
1) What would be the output for the following?
int main() {
Person mel (
Explanation / Answer
1. Output is: Name mel Age 30
weight is 95
2. Person class is not an abstract class because it doesn't have any abstract method.
3.
#include <iostream>
#include <string>
using namespace std;
class Person{
private:
string name;
double age;
public:
Person() {
name = "NoName";
age = 0;
}
Person(string newname, int newage) {
name = newname;
age = newage;
}
void setName(string newname) {
name = newname;
}
void setAge(int newage) {
age = newage;
}
virtual void display(ostream& out){
out << "Name " << name << " Age " << age << endl ;
}
};
class Girl: public Person{
private:
int weight;
public:
Girl( ):Person("Heidi", 15){
weight = 100;
}
Girl(string newname, int newage, int newweight ){
weight = newweight;
}
void display(ostream& out) {
cout << "weight is " << weight << endl;
}
};
class Boy: public Person {
private:
string eyecolor;
public:
Boy(string name, string color) {
eyecolor = color;
name = name;
age = 0;
}
Boy(string name, string color,int age) {
eyecolor = color;
name = name;
age = age;
}
void display() {
cout << cout << "Name " << name << " Age " << age << " eyecolor " << eyecolor << endl ;
}
};
int int main(int argc, char const *argv[])
{
Boy bb("Roy", "green", 3);
bb.display();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.