Inheritance from the base of a base class - c++

I'm learning c++ here.
I'm learning more about inheriting and classes but I'm having the following issue:
class A
{
public:
std::vector<int> vect1;
};
class B : A
{
public:
int x, y;
};
class C : B
{
c()
{
x=10;
y=30;
vect1.pushback(44);
}
};
why cant i access vect1 from the class C? how can i achieve this?
i also tried:
class C : B, A {...}
but then it said vect1 is ambiguous (or something like that). im using visual studios.
thank you.

why cant i access vect1 from the class C?
Because vect1 has private access. Sub classes can not access private members of a base class. They can only access protected or public members.
Furthermore, A is a private base of B, so sub classes of B have no access to it. Only protected or public bases are accessible to grand children.
The default access specifier of classes defined with the class keyword is private (The default is public for classes defined with the keyword struct). To declare a base with another access specifier, you can put the access specifier keyword right before each base in the declaration:
class B : protected A
{
// ...
};
To declare members with another access specifier:
class A
{
protected:
std::vector<int> vect1;
};

To get access from derived to base class you should do both:
public inheritance
protected declaration
like the following:
#include <vector>
class A
{
protected:
std::vector<int> vect1;
};
class B : public A
{
protected:
int x, y;
};
class C : public B
{
public:
C(){
x=10;
y=30;
vect1.push_back(44);
}
};

Related

Multilevel protected Inheritance in C++

I'm having a scenario where i'm implementing multilevel inheritance but first level inheritance is specified as protected inheritance , but it is giving me compilation issue.
class A
{
protected:
int a1;
};
class B: protected A
{
protected:
int b1;
};
class C: public B
{
public:
C()
{
a1=10;
b1=20;
cout<<a1<<b1<<endl;
}
};
int main()
{
C c; //can access A class protected data
A* a= new C; //compilation error: cannot cast 'C'to its protected base class 'A'
}
My issue is when i do inheritance using protected access specified ,I'm able to access all A Class data members then why can't I'm able to have a pointer of A class type holding C object?
main is not privy to the inheritance relationship due to its protected status.
class B: protected A
is what makes it inaccessible outside of B:: and C::

A confusion about c++ inheritance

when using inheritance, I read from a tutorial that says
Conversely, if the most restricting access level is specified (private), all the base class members are inherited as private and thus cannot be accessed from the derived class.
So I test following code but the derived class can still access the member of base class.
Why?
#include <iostream>
using namespace std;
class Polygon {
protected:
int width, height;
public:
Polygon():width(10),height(10){}
};
class Rectangle: private Polygon {
public:
int area ()
{ return height; }
};
int main () {
Rectangle rect;
cout << rect.area() << '\n';
return 0;
}
output is 10
The derived type can access public and protected members of the base class. This is independent of the type of inheritance. But the members are all private in the derived type if the inheritance is private.
class Foo
{
public:
int i;
};
class Bar : private Foo
{
public:
void hello() {
++i; // OK, I can see Foo's public and protected members
}
};
int main()
{
Bar b;
b.hello(); // accesses i internally. OK
b.i; // Error, i is private in Bar
}
Private inheritance means that all the base members become private to the derived class. The derived class itself can still access the members, but no further derived classes can.
According to the C++ Standard
If a class is declared to be a base class for another class using the
private access specifier, the public and protected members of the
base class are accessible as private members of the derived class117.
So in your example protected data members of the base class
protected:
int width, height;
are accessible as private members of the derived class and any member function of the derived class can access its private dara members.

How to assign and retrive the value in member variable?

I am put/get value in/from subclass B from object of base class A. But I am not able to assign or get the value. My code is:
class A
{
};
class B: A
{
string SID;
};
class C: A
{
string Name;
};
class D : A
{
string Name;
};
class E
{
A a;
UINT32 AccessLevel;
};
.......
main()
{
E e;
}
Using object of e am trying to get the value of subclass B.
I need to get the SID from the class B?
Thanks,
The C++11 standard 11/3 says:
Members of a class defined with the keyword class are private by default.
at 11.2/2
In the absence of an access-specifier for a base class [...] private is assumed when the class is defined with the class-key class.
and at 11.2/1:
If a class is declared to be a base
class for another class using the private access specifier, the public and protected members of the base
class are accessible as private members of the derived class.
So what does that mean? First of all:
class A {};
class B : A {};
Here A, by virtue of 11.2/2 is inherited privately. This may be okay if you want to inherit variables and you want to implement getter/setters for a variable only in a derived class, but that's usually considered bad style.
In your case however, as stated by 11/3, your members are not inherited at all because they are private members:
class A
{
public:
int a; // inherited
protected:
int b; // inherited
private:
int c; // NOT inherited
};
and especially
class A { int a; };
is equivalent to
class A { private: int a; };
So you could make your members accessable from within your derived classes by making them public or protected (see 11.2/1):
class A { public: int a; };
class B : A {}; // privately inherits a
and if you wanted to make it acessable from outside of your derived classes you will have to inherit as public as well:
class A { public: int a; };
class B : public A {}; // publicly inherits a
but that's not what you usually would do. It's considered better style to make variables private and expose only getters and setters for those:
class A
{
public:
int get_a() const { return a_; }
void set_a(int val) { a_ = val; }
private:
int a_;
};
class B : public A {}; // now publicly inherits the getters and setters
// but not a_ itself

Diamond inheritance with mixed inheritance modifers (protected / private / public)

let's say we have class A,B,C,D where A is base, B,C are between and D is derived in diamond model.
NOTE:
class B inherits virtualy class A in private mode,
class C inherita virtualy class A in protected mode.
class A
{
public:
int member; // note this member
};
class B :
virtual private A // note private
{
};
class C :
virtual protected A // note protected
{
};
class D :
public B, // doesn't metter public or whatever here
public C
{
};
int main()
{
D test;
test.member = 0; // WHAT IS member? protected or private member?
cin.ignore();
return 0;
}
now when we make an instance of class D what will member be then? private or protected lol?
Figure No2:
what if we make it so:
class B :
virtual public A // note public this time!
{
};
class C :
virtual protected A // same as before
{
};
I suppose member will be public in this second example isn it?
§11.6 Multiple access [class.paths]
If a name can be reached by several paths through a multiple inheritance graph, the access is that of the path that gives most access. [ Example:
class W { public: void f(); };
class A : private virtual W { };
class B : public virtual W { };
class C : public A, public B {
void f() { W::f(); } // OK
};
Since W::f() is available to C::f() along the public path through B, access is allowed. —end example ]
I think I don't need to add anything else, but see also this defect report (which was closed as "not a defect").

how can a derived class function call a function of the base class?

Derived class function cannot access even the public members of the base class when the access specifier is private. But how is it that the function 'xyz' of my derived class able to call 'showofb'?
I even tried it by calling the function 'showofb' in the constructor of C. In both cases it works.
How is it able to call the function 'showofb' ?
class B
{
public:
B()
{
cout<<":B:"<<endl;
}
void showofb()
{
cout<<"show of b"<<endl;
}
};
class C : private B
{
public:
C()
{
cout<<":C:"<<endl;
}
void xyz()
{
showofb();
}
};
int main()
{
C c1;
c1.xyz();
}
Private inheritance inherits the public members of the parent as the private members of the child. A class can call its own or inherited private members.
Consider this:
class B
{
public:
B()
{
cout<<":B:"<<endl;
}
void showofb()
{
cout<<"show of b"<<endl;
}
};
class C : private B
{
public:
C() {}
};
class D : public B
{
public:
D(){};
}
int main()
{
C c1;
c1.showofb(); // WONT WORK
D d1;
d1.showofb(); // WILL WORK
}
B::showofb() is a public function. So it can be called by C. If you modify B to make showofb private, C will no longer be able to call it.
The private inheritance means that all public and protected members of B are inherited as private by C. So C can still call public and protected members of B, but any classes derived from C will not be able to call members of B.
user1001204, you appear to have a mistaken concept of private inheritance. That class C inherits from class B via private inheritance means that the inheritance relationship is hidden to anything that uses class C. Private inheritance does not hide the inheritance relationship inside Class C itself.