Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi eveeryone,
Can someone please look at my code and teach me where i am making mistake.
C++
 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;	
	
 
}

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:
C++
#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();
}	
Posted

OK change these lines:

C++
#include <iostream>

C++
// Inherit Laptop from Computer
class Laptop: public Computer

C++
//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)

C++
// Not needed
//	clrscr();


Now compile the program.
 
Share this answer
 
v3
Your Laptop class is not derived from the Computer class because you missed the base class declaration in the derived class definition. It should be:
C++
class Laptop : public Computer
{
// ...
};
 
Share this answer
 
Thank You guys... That was the thing i missing.Thank You soo much for your precious time
@pwasser and @Jochen Arndt
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Nov-15 10:10am    
Nice words, but they should go to the comments to the answers or other posts. This is not a solution; posting the content which is not intended to help people having a problem formulated in a question is considered as abuse. Besides, people you are talking to won't get notifications.
Thank you for understanding.
—SA

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