954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Inheritance Class ...... help needed asap

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:

raj157
Light Poster
41 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

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
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

raj157
Light Poster
41 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

post code please.

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

raj157
Light Poster
41 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

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

raj157
Light Poster
41 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

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
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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

raj157
Light Poster
41 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

I meant, do something like:

Person obj(nam1, dat1);
obj.print();  // call the member function print
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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

raj157
Light Poster
41 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

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
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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;
}
raj157
Light Poster
41 posts since Mar 2007
Reputation Points: 10
Solved Threads: 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() { }
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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

raj157
Light Poster
41 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

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

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

i just added the two lines like u mentioned

raj157
Light Poster
41 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

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
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

#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
raj157
Light Poster
41 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

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
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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

raj157
Light Poster
41 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You