Well it is an assignment question here is the question :

Implement a base class Person. Derive classes Student and Instructor from Person. A person has
a name and a birthday. A student has a major, and an instructor has a salary. Write the class
definitions, the constructors, and the member functions print() for all classes.

this is what i have got so far any kind of help will be good :

#include <iostream>
using namespace std;

class Person{
public: 
    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<<" Mr/Mrs."<<name<<" was born on "<<date<<"\n";}

int main (){
    string nam1;
    string dat1;
    cout<< " Please enter the name of student  \n";
    cin>>nam1;
    cout<< " Please enter the date of birth in the format dd/mm/yyyy \n";
    cin>>dat1;
    Person(nam1,dat1);
    return 0;
}

Well i have managed to get the base class person but it gives error can someone help me out with whats wrong and how to implement the other required derived class student and instructor :idea:

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 <string> 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
};

well i did tht ... still does not work .. gives error of one or multiple defined symbols found .....

post code please.

#include <iostream>
#include <string>
using namespace std;

class Person{
    public: 
        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<<" Mr/Mrs."<<name<<" was born on "<<date<<"\n";}

int main (){
    string nam1;string dat1;
    cout<< " Please enter the name of student  \n";
    cin>>nam1;
    cout<< " Please enter the date of birth in the format dd/mm/yyyy \n";
    cin>>dat1;
    Person(nam1,dat1);
    return 0;
}

well i got it running but now i cant get it to print the output .. can anyone help me asap... thanks

You need to call the print function after creating the object. Something along the lines of obj.print().

dint learn yet about object.print ... have only learned about the print in the class like i have done in my code.

I meant, do something like:

Person obj(nam1, dat1);
obj.print();  // call the member function print

i am confussd in how to call the print function. lets say the class name is Person then how would u print it .... i mean what command would u type

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.

can someone tell me whats wrong with this code .. it gives error of not finding default constructor for the class person .. can someone help me out ... and is this the correct way of approaching the assignment .. if not any kind of input would be good enough

here is the code

#include <iostream>
#include <string>
using namespace std;

class Person{
public:
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(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;
}

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() { }

it still gives the same error and gives see declaration of student and overloaded member function not found in person

Post the most recent code and the lines which flag an error.

i just added the two lines like u mentioned

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;
}
#include <iostream>
#include <string>
using namespace std;

class Person{
public:
    Person:: 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(){}
        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::Instructor(){}
        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;
}

it still gives the same error

Compiling...qt4.cppd:\my documents\visual studio 2005\projects\anu\anu\qt4.cpp(40) : error C2512: 'Person' : no appropriate default constructor available
d:\my documents\visual studio2005\projects\anu\anu\qt4.cpp(61) : error C2512: 'Person' : no appropriate default constructor available

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.

how would you copy some datat from the class person to instructor .. like i want to copy the name and date of birth from the person class and use it in instructor class

#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 Instructor : public Person
{
public:
    Instructor () {}
    Instructor(double sal);
    double get_salry() const;
    string get_infoname()const;
    string get_infodate1()const;
    void print()const;
private:
    double salry;
    string date1;
    string namei;
};

Instructor::Instructor(double sal)
{
    salry=sal;
}

double Instructor::get_salry() const
{
    return salry;
}

string Instructor::get_infoname()const
{
    namei=Person::get_name();
    return namei;
}

string Instructor::get_infodate1()const
{
     date1=Person::get_date();
     
    return date1;
}

void Instructor::print()const
{
    system("cls");
    cout <<"============================================\n";
    cout <<"Instructor Name   : " <<namei <<"\n"; 
    cout <<"Instructor D.O.B  : " <<date1<<"\n";
    cout <<"Instructor Salary : " <<salry<<"\n";
    cout <<"============================================\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 instructor \n";
    cout<< " 0 for none of the above \n";
    cin>>val;

   

    if (val==1)
    {
        system("cls");
        cout<<" Please enter the Instructor's Salary:\n";
        cin>>sal1;
        Instructor obj(sal1);
        obj.print();
    
    }

    if (val==0)
    {
        Person obj(nam1,dat1);
        obj.print();}



    return 0;
}

i tried doing the above it gives me error

YOu don't need to redefine the member variables name and date of birth inside the Instructor class since Instructor inherits those from the Person class. Just make a Instructor which takes in three parameters and inside that call the constructor of Person class which takes in two parameters and you would be good to go. Also before asking questions, consider doing some research and studying rather than trying the syntax on your own.

Read this and this.

Hi,

Let me explain that to you.
The problem is because of the following code.

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();
}

Explaination:
Inheritance means the child class will get all the properties from parent class.
like a father and son, father's surname will become son's surname automatically.

suppose son's name is "sanjay rawat" and we call him "Sanjay" then we can not identify that we are talking about "sanjay rawat". Right?

In the same way, when creating a child class object, we will need to provide all the information.

here parent class is person with 2 fields name and date.
also you have declared a constructor with 2 arguments.
child class "student" has 1 field major.
when you call the constructor of child class you will need to provide information for all the three fields.

you are creating object of student class as

student obj(maj1);

in this case you are calling parent constructor without any argument, which you have not provided.
so, giving error.

Thanks,
Vivek

To be honest, I dont think he cares anymore.. His last activity was on Jun 5th, 2007.

commented: That was one hilarious post... it completely made my day. +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.