#include<iostream.h>

class A
{
public:
read()
{cout<<"read";
}};
class B:public A
{public:
readb()
{
cout<<"readb";
}
};
class C:public A
{
public:
readc()
{cout<<"readc";
}
};
class D:public B,public C
{
public:
readd()
{
cout<<"readd";
}
};
main()
{
D d;
//d.read();
d.readb();
}
am getting error for this...

Recommended Answers

All 6 Replies

1) Use code tags.
2) include <iostream> instead of <iostream.h>
3) prefix cout with std::
4) readb,c, and d must be void

#include<iostream.h>

class A
{
public:
read()
{cout<<"read";
}};
class B:public A
{public:
readb()
{
cout<<"readb";
}
};
class C:public A
{
public:
readc()
{cout<<"readc";
}
};
class D:public B,public C
{
public:
readd()
{
cout<<"readd";
}
};
main()
{
D d;
//d.read();
d.readb();
}
I think you get errors sice you did not overloaded input and out put redirectional operators

#include<iostream.h>

class A
{
public:
read()
{cout<<"read";
}};
class B:public A
{public:
readb()
{
cout<<"readb";
}
};
class C:public A
{
public:
readc()
{cout<<"readc";
}
};
class D:public B,public C
{
public:
readd()
{
cout<<"readd";
}
};
main()
{
D d;
//d.read();
d.readb();
}
I think you get errors sice you did not overloaded input and out put redirectional operators

Actually errors in d sense...
readb has declared under public section but am unable to access that through the derived class object d.
d.readb();//is ambiguous

readb has declared under public section but am unable to access that through the derived class object d.
d.readb();//is ambiguous

read() is ambiguous, readb() should not be. You created the Diamond Problem where the most derived class has two copies of the same base object. Virtual inheritance fixes this problem by forcing only a single copy of the shared base class.

I fixed other problems that kept your code from compiling on my newer compiler, so it may not compile for you until you change the header back to <iostream.h> and remove the using namespace std; line. The rest of the code should continue to work on an old compiler:

#include <iostream>

using namespace std;

class A
{
public:
    void read()
    {
        cout << "read\n";
    }
};

class B: virtual public A
{
public:
    void readb()
    {
        cout << "readb\n";
    }
};

class C: virtual public A
{
public:
    void readc()
    {
        cout << "readc\n";
    }
};

// B and C share an instance of A
class D: public B, public C
{
public:
    void readd()
    {
        cout << "readd\n";
    }
};

int main()
{
    D d;
    d.read();
    d.readb();
    d.readc();
    d.readd();
    return 0;
}

check this out guys......

#include<iostream.h>
#include<conio.h>
class A
{
protected:
int a;
public:
void r1()
{
cout<<"enter a num";
cin>>a;
}
};
class B :: public A
{
protected:
int b;
public:
void r2()
{
cout<<"enter a num";
cin>>b;
}
void s1()
{
cout<<a+b;
}
};
class C :: public A
{
protected:
int c;
public:
void r3()
{
cout<<"enter a num";
cin>>c;
}
void s2()
{
cout<<a+c;
}
};
class D :: public B,public C
{
int d;
public:
void r4()
{
cout<<"enter a num";
cin>>d;
}
void s3()
cout<<a+b+C+d;
}
};
void main()
{
A a1;
a1.r1();
B b1;
b1.r1();
b1.r2();
b1.s1();
C c1;
c1.r1();
c1.r3();
c1.s2();
D d1;
d1.r1();
d1.r2();
d1.r3();
d1.r4();
d1.s1();
d1.s2();
d1.s3();
getch();
}
commented: Not sure why you're bragging, this is all kinds of wrong. -1

>check this out guys..
Why? It doesn't address the original poster's question and this thread is from 2009.

Did you have a point in posting this code? If you did, please state it.

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.