在子类成员函数Pin中调用基类memeber函数 [英] Calling base class memeber function in child class member function Pin
本文介绍了在子类成员函数Pin中调用基类memeber函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨eveeryone,
有人可以看看我的代码并教我错误的地方。
Hi eveeryone,
Can someone please look at my code and teach me where i am making mistake.
void Laptop::show()
{ Computer::show();
cout<<"weight is="<<weight<<endl;
cout<<"height is ="<<height<<endl;
cout<<"length is ="<<length<<endl;
cout<<"width is ="<<width<<endl;
}
在这个函数中,Laptop :: show是一个子类函数,它调用Computer :: show();基类function.but编译器使用错误.or - >函数Computer :: show();未被调用。
原始代码如下:
In this function Laptop::show is a child class fucntion which is calling Computer::show(); a base class function.but compiler gives error to use .or -> so function Computer::show(); is not called.
Orignal code is given below:
#include<iostream.h>
#include<conio.h>
class Computer
{
protected:
int wordSize;
int memorySize;
int storageSize;
int speed;
public:
Computer(){}
Computer(int,int,int,int);
void show();
};
class Laptop
{
private:
int width;
int height;
int length;
int weight;
public:
Laptop(){}
Laptop(int,int,int,int,int,int,int,int);
void show();
};
Computer::Computer(int wdSize, int memSize, int storSize ,int spee)
{
wordSize=wdSize;
memorySize=memSize;
storageSize=storSize;
speed=spee;
}
void Computer::show()
{
cout<<"Word Size :"<<wordSize<<endl;
cout<<"Memory Size: "<<memorySize<<endl;
cout<<"Storage Size"<<storageSize<<endl;
cout<<"Speed :"<<speed;
}
Laptop::Laptop(int wdSize, int memSize,int storSize, int spee,int wid, int hei , int len , int wait ):Computer(wdSize,memSize,storSize,spee);
{
width=wid;
length=len;
height=hei;
weight=wait;
}
void Laptop::show()
{ Computer::show();
cout<<"weight is="<<weight<<endl;
cout<<"height is ="<<height<<endl;
cout<<"length is ="<<length<<endl;
cout<<"width is ="<<width<<endl;
}
void main()
{
clrscr();
Computer comp(4,512,20,2);
Laptop lap(8,1024,50,2,15,19,14,2);
cout<<"Computer Specifications are"<<endl;
comp.show();
cout<<":Laptop specifications are"<<endl;
lap.show();
getch();
}
推荐答案
确定改变这些行:
OK change these lines:
#include <iostream>
// Inherit Laptop from Computer
class Laptop: public Computer
//Remove semicolon
Laptop::Laptop(int wdSize, int memSize,int storSize, int spee,int wid, int hei , int len , int wait ):Computer(wdSize,memSize,storSize,spee)
// Not needed
// clrscr();
现在编译程序。
Now compile the program.
你的笔记本电脑
类不是从计算机<派生的/ code> class因为您错过了派生类定义中的基类声明。它应该是:
YourLaptop
class is not derived from theComputer
class because you missed the base class declaration in the derived class definition. It should be:
class Laptop : public Computer
{
// ...
};
谢谢你们......那是我想念的东西。感谢你们的宝贵时间。
@pwasser和@Jochen Arndt
Thank You guys... That was the thing i missing.Thank You soo much for your precious time
@pwasser and @Jochen Arndt
这篇关于在子类成员函数Pin中调用基类memeber函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文