Can anyone please tell me what is the problem in my code:

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;  


}

I am unable to call
Computer::show(); function in Laptop::show(); function.Compiler says to use . or -> operator i am unable to understand.
Computer is base class and laptop is derived class.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();
}   

First off in order to have Computer the base class for Laptop you need to declare it as such:

class Laptop : Computer

The problem you have now is naming the Laptop show function the same as the Computer show function. The compiler can't see past the Laptop show function. You'll be better off renaming that function to something more descriptive, like showComputer, and the laptop function to showLaptop. This way the compiler will allow access to both functions separately.

void Laptop::showLaptop()
{
    showComputer();
    cout << "weight is=" << weight << endl;
    cout << "height is =" << height << endl;
    cout << "length is =" << length << endl;
    cout << "width is =" << width << endl;
}

Or ... maybe ... something like in this recent example ... is what you are aiming to do ?

https://www.daniweb.com/programming/software-development/threads/500762/need-help-starting-this-program#post2189803

You make like to see this modification to the demo ... that also shows the destructors being called.

Note the use of new / delete here ... and the use of 'pointers' ...
(i.e an array of pointers.)

// airCraft2.cpp //

#include <iostream>


using namespace std;


class AirCraft
{
public:
    AirCraft( int e = 1 , int s = 1 ) : engines(e), seats(s) {}
    int get_engines() const { return engines; }
    int get_seats() const { return seats; }

    virtual ~AirCraft() { cout << "AirCraft dtor was called ...\n"; }

    virtual void print() const = 0;
private:
    int engines;
    int seats;
} ;


class Fighter : public AirCraft
{
public:
    Fighter( int e=1, int s=1, int g = 1 ) : AirCraft::AirCraft( e, s ), guns(g) {}

    ~Fighter() { cout << "Fighter dtor was called ...\n"; }

    void print() const
    {
        cout << "engines = " << get_engines()
             << ", seats = " << get_seats()
             << ", guns = " << guns << endl;
    }
private:
    int guns;
} ;

class Freight : public AirCraft
{
public:
    Freight( int e=2, int s=2, int w = 2000 ) : AirCraft::AirCraft( e, s ), lbs(w) {}

    ~Freight() { cout << "Freight dtor was called ...\n"; }

    void print() const
    {
        cout << "engines = " << get_engines()
             << ", seats = " << get_seats()
             << ", lbs = " << lbs << endl;
    }
private:
    int lbs;
} ;



void test_it_out()
{
    AirCraft* a [10]; // holds pointers to 'base'class
    a[0] = new Fighter(); // pointer to (new) derived class obj
    a[1] = new Freight( 4, 2, 10000 ); // pointer derived class obj

    a[0]->print(); // Knows 'which print' to call (Fighter)
    a[1]->print(); // Knows 'which print' to call (Freight)

    delete a[1];
    delete a[0];
}



int main()
{
    test_it_out();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.