well the problem is i want to write the person's name from preson class to instructor class using inhertiance, i have come up with the following code but gives error .. can someone tell me what am i missing or what can be done. its like sharing from person class to instructor class thats what i need...

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

Recommended Answers

All 8 Replies

???????????????????

>i have come up with the following code but gives error
Maybe, just maybe you might want to tell us what those errors are :icon_rolleyes:

there u go

d:\my documents\visual studio 2005\projects\anu\anu\ver1.cpp(67) : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion)

d:\my documents\visual studio 2005\projects\anu\anu\ver1.cpp(73) : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion)

Since there's no line numbers on your code, how do you expect us to know which lines those errors correspond to?

Anyway, I gave up and ran it through my compiler. The problem is that you're trying to convert a const string (returned from Person::get_name and Person::get_date) to a regular string. Why are you copying the variables in your accessor functions anyway? Just let the base class do all the dirty work.

[edit]
arrgh I see you've created multiple threads. Just do what ~s.o.s~ said.

so how can i access the name and date

>so how can i access the name and date
Something like this?

Instructor myInstructor;

// ...

myInstructor.get_name(); // this calls Person::get_name()

The point of polymorphism is that it saves you from writing identical pieces of code over and over again. So then don't.

>so how can i access the name and date
Something like this?

Instructor myInstructor;

// ...

myInstructor.get_name(); // this calls Person::get_name()

The point of polymorphism is that it saves you from writing identical pieces of code over and over again. So then don't.

still lost ... can you show it in the code if u don't mind

thnks figured it out

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.