#include<iostream>
using namespace std;
class B
{
    int a;
public:
    int b;
    void get_ab();

    int get_a();
    void show_a();
};
class D :public B
{
    int c;
public:
    void mul();
    void display();
};
void B::get_ab()
{
    a=5;b=10;
}
int B::get_a()
{
    return a;
}
void B::show_a()
{
    cout<<"a="<<a<<"\n";
}

void D::mul()
{
    c= b * get_a();
}

void D::display()
{
    cout<<"a="<<get_a()<<"\n";
    cout<<"b="<<b<<"\n" ;
     cout<<"c="<<c<<"\n" ;
}
int main()
{
    D d;
    d.get_a() ;
    d.get_ab();
    d.show_a();
    d.display();
    d.b=20;
    d.mul();
    d.display()
    return 0;


}

OUTPUT::
a=5
a=5
b=10
c=50

a=5
b=20
c=100

why in 1st output a=5
a=5
again...

Recommended Answers

All 2 Replies

The first time a is shown on screen is when you call the function d.show_a();

The second time a is shown is when you call d.display();

I know its not required , but totally agree with @Moschops.

I wonder why you didnt notice, that you did not call, d.show_a() and only d.display(), in the next output sequence, and hence had one 'a=' on the console.

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.