Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Everyone!

please tell me what is the problem in the code because when i run this program it only show the Base Class Output and did not show the Drived Class Output please tell me if any one know about this.

C++
#include <iostream>
using namespace std;
class Practice
{
protected:
    int ID,Pay;
    string Name;
public:
    Practice()
    {
        cout<<"Now The Object Is Created."<<endl;
        ID=0;
        Pay=0;
        Name="";
    }
    //Here Is Prametrized Constructor
    Practice(int I,int P,string N)
    {
        ID=I;
        Pay=P;
        Name=N;
    }
    void GetData()
    {
        cout<<"Employee Name:"<<Name<<"\n\nEmployee ID:"<<ID<<"\n\nEmployee Pay:"
        <<Pay<<endl;
    }
};
//Now Going To Create Second Class For Inharitance
class Pay_Roll:public Practice
{
private:
    int Salary;
public:
    //Create Constructor For Accessing The Base Class Value
    Pay_Roll(int i,int d,string n,int S)
    :Practice(ID,Pay,Name)
    {
        Salary=S;
    }

    //Now Used Constructor Foe Second Employee Data
    int Ids()
    {
        return ID;
    }
    void GetData()
    {
//        stringstream SS;
        cout<<"Employee Name:"<<Name<<"\n\nEmployee ID:"
        <<ID<<"\n\nEmployee Pay:"
        <<Pay<<"\n\nEmployee Salary:"<<Salary
        <<endl;
    }
};
int main()
{
    Practice Obj1(12,35000,"Muhammad Qasim");
    Obj1.GetData();
    Pay_Roll obj2(13,40000,"Muhammad Usman",50000);
    obj2.GetData();
//    cout<<obj2.Ids();
    return 0;
}
Posted
Updated 6-Nov-14 9:31am
v2

Look at these two lines VERY carefully. What are you passing in? What are you sending to the base class constructor?

C++
Pay_Roll(int i,int d,string n,int S)
:Practice(ID,Pay,Name)


PS - please add <pre lang="C++"> ... </pre> around your code in the future, as I modified your question to.
 
Share this answer
 
You should declare the methods you wish to override in the derived classes as virtual. There is no "default virtual" in C++ (I use C++98 so maybe it has been introduced recently).
 
Share this answer
 
#include <iostream>
using namespace std;
class Practice
{
protected:
int ID,Pay;
string Name;
public:
//New Syntax Of Default Constructor
Practice():ID(0),Pay(0),Name(""){}
//Here Is Prameterized Constructor
Practice(int I,int P,string N)
{
ID=I;
Pay=P;
Name=N;
}
~Practice()
{
}
void GetData()
{
cout<<"Employee Name:"<<Name<<"\n\nEmployee ID:"<<ID<<"\n\nEmployee Pay:"
<<Pay<<endl;
}
//Destructor
};
//Now Going To Create Second Class For Inharitance
class Pay_Roll:public Practice
{
private:
int Salary;
public:
//Create Constructor For Accessing The Base Class Value
Pay_Roll(int ID,int Pay,string Name,int S)
:Practice(ID,Pay,Name)
{
Salary=S;
}
~Pay_Roll()
{
}
//Now Used Constructor Foe Second Employee Data
// int Ids()
// {
// return ID;
// }
void GetData()
{
// stringstream SS;
cout<<"\n\nEmployee Name:"<<Name<<"\n\nEmployee ID:"
<<ID<<"\n\nEmployee Pay:"
<<Pay<<"\n\nEmployee Salary:"<<Salary
<<endl;
}
};
//Now Make Another Derived Class With Base Class
//class Show_Class:public Practice
//{
// public:
// TakeData(int s)
// :Practice(ID,Pay){};
//};
int main()
{
Practice Obj1(12,35000,"Muhammad Qasim");
Obj1.GetData();
Pay_Roll obj2(13,40000,"Muhammad Usman",50000);
obj2.GetData();
return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900