Question: About Inheritance, Constructors, and Function Overriding Write a C++ p
ID: 3677866 • Letter: Q
Question
Question: About Inheritance, Constructors, and Function Overriding
Write a C++ program that contains the following parts. Write each class interface and implementation, in a different .h and .cpp file, respectively.
a) Create a class called "Student" that has a name (string) and ID (int) as member variables.
b) Create a derived class called "ComputerSystemsStudent" from Student class. ComputerSystemsStudent has also a track (string) as member variable. A track take values such a "Programming", "Web Design", "Networking", etc.
c) Write a constructor with all default parameters for Student and ComputerSystemsStudent classes.
d) Write printInfo() function for Student and ComputerSystemsStudent classes. -- For ful credit, ComputerSystemsStudent class must overridde the printInfo of the Student class.
e) Create a test program that includes one to two objects Student ComputerSystemsStudent classes and write test code print information about each object.
( note that you need submit a total of five files )
Explanation / Answer
#include <iostream>
using namespace std;
class Student
{
public:
int id;
string name;
Student(){//default constructor
id=101;
name="arul";
}
Student(int num,string s)//parameterised constructor
{
id=num;
name=s;
}
void PrintInfo()
{
cout << "StudentId:"<<id<<" Student Name:"<<name<<endl;
}
};
class ComputerScienceStudent:public Student
{
public:
string track;
ComputerScienceStudent()//default constructor
{
track="networking";
}
ComputerScienceStudent(string t)//parameterised constructor
{
track=t;
}
void PrintInfo()
{
cout << "Student track"<<track<<endl;
}
};
int main()
{
Student *s1;
ComputerScienceStudent cs("programming");
s1=&cs;//base class pointer refering to object of derived class
s1->PrintInfo();//calls base class method
cs.PrintInfo();//calls derived class method
}
output:
StudentId:101 Student Name:arul
Student trackprogramming
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.