First learn how to properly format your programs. I helped you out this time by doing it for you just so you can see how it should be done. There is absolutely nothing wrong with being generous with spaces and line feeds -- makes your program a lot easier to read.
>> it gives error
what error? I suspect it might be because you did not include header file.
>>and how to implement the other required derived class student and instructor
here is how to declare Student. Other derived classes are the same format.
class Student : public Person
{
// your code here
};
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
You need to call the print function after creating the object. Something along the lines of obj.print().
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
I meant, do something like:
Person obj(nam1, dat1);
obj.print(); // call the member function print
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Maybe you didn't read my previous post. It does exactly what you are asking for i.e. invoking the print function of the instance of class Person.
Maybe you ought to read this.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Normally C++ provides the default constructor for any class you create but once you decide to create your own constructors, it is mandatory to provide a default constructor on your own. Its like when you deny a free ride, you have to walk all the way home on your own.
Add the following lines of code:
Person::Person() { }
Student::Student() { }
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Post the most recent code and the lines which flag an error.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
The place where you add them, makes all the difference in the world. Posting the code would have made it really clear. Please do as requested to get maximum help. BTW here is the working code, I just made the additions in the right place. ;)
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person() { }
Person( string nam, string day);
string get_name()const;
string get_date()const;
void print()const;
private:
string name;
string date;
};
Person::Person( string nam,string day)
{
name= nam;
date= day;
}
string Person::get_name() const
{
return name;
}
string Person::get_date() const
{
return date;
}
void Person::print()const
{
cout<<"\nMr/Mrs: "<<name<<" was born on "<<date<<"\n";
}
class Student : public Person
{
public:
Student() {}
Student(string maj);
string get_major()const;
void print()const;
private:
string major;
};
Student::Student(string maj)
{
major=maj;
}
string Student::get_major() const
{
return major;
}
void Student::print() const
{
cout<<"Studying : "<<major<<"\n";
}
class Instructor : public Person
{
public:
Instructor(double sal);
double get_salry() const;
void print()const;
private:
double salry;
};
Instructor::Instructor(double sal)
{
salry=sal;
}
double Instructor::get_salry() const
{
return salry;
}
void Instructor::print()const
{
cout<<"Earns : "<<salry<<"\n";
}
int main ()
{
string nam1;
string dat1;
string maj1;
int val;
double sal1;
cout<< "Please enter the name of Person \n";
getline (cin,nam1);
cout<< "Please enter the date of birth in the format dd/mm/yyyy \n";
cin>>dat1;
cout<< " Please Enter one of the below choices \n 1 for student \n 2 for instructor \n";
cout<< " 0 for none of the above \n";
cin>>val;
Person obj(nam1,dat1);
obj.print();
if (val==1)
{
cout<<" Please enter the Students Major:\n";
getline(cin,maj1);
Student obj(maj1);
obj.print();
}
if (val==2)
{
cout<<" Please enter the Instructor's Salary:\n";
cin>>sal1;
Instructor obj(sal1);
obj.print();
}
return 0;
}
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Copy and paste the code posted by me in the previous post. It works. You don't need classname qualifiers in front of member functions when written inside classes.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734