[C++复习]05| inheritance

[note 1]

Protected derivation

  • Both the public and protected members of the base class become protected members of the derived class.

[note 2]

Ambiguity Resolution in inheritance

#include<iostream>
using namespace std;

class A
{
public: 
	void f(){cout<<"From  A"<<endl;}
};

class B
{
public: 
	void f(){cout<<"From  B"<<endl;}
};

class MI: public A, public B
{
public:
	void g(){cout<<"From  MI"<<endl;} 
};

void main()
{ 
    MI mi;
//    mi.f();				//Won't work!
    mi.A::f();			    //Correct
} 

[note 3]

Virtual base class

#include <iostream>
using namespace std;

class student
{
protected:
	int roll_number;
public:
	void get_number(int a){roll_number=a;}
	void put_number(void){cout << "Roll No: " << roll_number << "\n";}
};

class test:virtual public student
{
protected:
	float part1,part2;
public:
	void get_marks(float x, float y){part1=x; part2=y;}
	void put_marks(void){cout << "Marks obtained: " << "\n" << "Part1 = " << part1 << "\n" << "Part2 = " << part2 << "\n";}
};

class sports:public virtual student
{
protected:
	float score;
public:
	void get_score(float s){score=s;}
	void put_score(void){cout << "Sports wt: " << score << "\n\n";}
};

class result:public test, public sports
{
	float total;
public:
	void display(void);
};

void result::display(void)
{
	total=part1+part2+score;
	put_number();
	put_marks();
	put_score();
	cout << "Total Score: " << total << "\n";
}

int main()
{
	result student_1;
	student_1.get_number(678);
	student_1.get_marks(30.5,25.5);
	student_1.get_score(7.0);
	student_1.display();

	return 0;
}

[note 4]

Abstract class is not used to create objects (only to act as a base class).

[note 5]

In case of multilevel inheritance, the constructor will be executed in the order of inheritance.


#include <iostream>
using namespace std;
class A
{
public:
    A(){cout<<"A..."<<endl;}
};

class B:public A
{
public:
    B(){cout<<"B..."<<endl;}
};

class C:public B
{
public:
    C(){cout<<"C..."<<endl;}
};
int main(int argc, const char * argv[]) {
    C c;
    
    return 0;
}

output:

A...
B...
C...

[note 6]

In case of multiple inheritance, the base classes are constructed in the order in which they appear in the declaration of the derived class.

//
//  main.cpp
//  teat
//
//  Created by 邱锐鹏2 on 2019/6/18.
//  Copyright © 2019 邱锐鹏2. All rights reserved.
//

#include <iostream>
using namespace std;
class A
{
public:
    A(){cout<<"A..."<<endl;}
};

class B
{
public:
    B(){cout<<"B..."<<endl;}
};

class C:public B,public A
{
public:
    C(){cout<<"C..."<<endl;}
};
int main(int argc, const char * argv[]) {
    C c;
    
    return 0;
}

output:

B...
A...
C...

[note 7]

The order of construction:

  • constructor of the base class
  • constructing the object member
  • constructor of the derived class

[note 8]

Constructor of the derived class take responsibility only on the initialization of the base class that the derived class immediately derived from.

发布了51 篇原创文章 · 获赞 5 · 访问量 4207

猜你喜欢

转载自blog.csdn.net/qq_43519498/article/details/92778065
今日推荐