Subclass assigned to Parent class (C + + beginners)

Source: Internet
Author: User

Write while thinking

Explore the subclass object assigned to the parent class object #include <iostream>
using namespace Std;
Class A
{
Public:
int A;
int b;
};
Class B:public A
{
Public:
int A;
int b;
void Func ()
{
A::A = 100;//The control of the inherited part, by the way of the domain name, can only be done in the class
A::B = 200;
}
};
int main (void)
{
cout << sizeof (A) << Endl;
cout << sizeof (B) << Endl;
These two lines of code show that two data members in a and two data members in B are not related
A A;
b b;
A = b;
cout << a.a << endl << a.b << Endl;

B.func ();
A = b;
cout << a.a << endl << a.b << Endl;
The comparison between the two is very strong,
/*
As you can see, the assignment of a subclass to a parent class is actually a partial assignment from the parent class in the subclass object to the parent class object ...
Think about it, such a assignment, as if it is simply a function of the data members, as if there is no relationship with the member functions.
It is true that we can modify the data members inherited from the parent class in the subclass, and the member functions of the parent class. I don't think so.
Make any changes to it,
So
There is a relationship between the assignment of a subclass to a parent object, which is only a more data member.
There is the handover of authority.
*/
B. (a::a) a= 1000; Attempts to manipulate the members of a parent class in a subclass by way of a domain name are not possible outside of subclasses. Must be in the class
return 0;
}
/*
See a word "truncate", the modifier subclass is very good for the parent class object assignment.
That is, a new member object of a subclass is truncated when it assigns a value to the parent class object. Only the parent class member is left ...

*/


I'm happy to share what I want when I write the code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.