驱动程序类的C ++ Inharitance问题 [英] C++ Inharitance Problem For Driver Class

查看:73
本文介绍了驱动程序类的C ++ Inharitance问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone!



请告诉我代码中的问题是什么,因为当我运行这个程序时它只显示基类输出并且没有显示驱动类输出请告诉我是否有人知道这个。



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.

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

推荐答案

仔细看看这两行。你在路过什么?你发送给基类构造函数的是什么?



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

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





PS - 请添加< pre lang =C ++> ...< / pre>我将你的问题修改为你的代码。



PS - please add <pre lang="C++"> ... </pre> around your code in the future, as I modified your question to.


你应该将派生类中要覆盖的方法声明为虚拟。 C ++中没有默认虚拟(我使用C ++ 98,所以最近可能已经介绍过了。)
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).


#include< iostream>

using namespace std;

class Practice

{

protected:

int ID,Pay;

string Name;

public:

//默认构造函数的新语法

Practice():ID(0),Pay (0),名称(){}

//这里是Prameterized构造函数

练习(int I,int P,string N)

{

ID = I;

Pay = P;

Name = N;

}

~练习()

{

}

void GetData()

{

cout<<员工姓名:<<名称<<\ n \ n员工ID:<<<<<<\ n \ n员工支付:

<< Pay<< endl;

}

//析构函数

};

//现在去吧创造第二类吸引力

类Pay_Roll:公共实践

{

私人:

int薪水;

public:

//创建用于访问基类值的构造函数

Pay_Roll(int ID,int Pay,string Name,int S)< br $> b $ b:练习(身份证,工资,姓名)

{

薪水= S;

}

~Pay_Roll()

{

}

//现在使用的构造函数Foe第二个员工数据

// int Ids()

// {

//返回ID;

//}

void GetData()

{

// stringstream SS;

cout<<\ nn \ nEmployee名称:< <名称<<\ n \ n员工ID:

<< ID<<\ n \ n员工付款:

<< Pay<<\ n \ nEmployee Salary:<< Salary

<< endl;

}

};

//现在用基类创建另一个派生类

//类Show_Class:public Practice

// {

// public:

// TakeData(int s)

//:练习(ID,Pay ){};

//};

int main()

{

练习Obj1(12, 35000,Muhammad Qasim);

Obj1.GetData();

Pay_Roll obj2(13,40000,Muhammad Usman,50000);

obj2.GetData();

返回0;

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


这篇关于驱动程序类的C ++ Inharitance问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆