目录
一、继承的关系
二、继承方式和子类权限
三、子类构造函数
四、继承的种类
一、继承的关系
继承一定要的关系:子类是父类 学生是人 狗是动物
继承的实现形式:
class 子类名:继承方式 父类名
{
成员变量:
成员函数:
} ;
二、继承方式和子类权限
继承方式:public protected private
父类 | 继承方式 | 子类 |
Public | Public | Public(类内类外都可以访问) |
Protected | Protected(类内可以访问) | |
Private | 不可访问 |
#include<iostream>
using namespace std;
/****************父类********************/
class Base
{
public:int c;
private:char a;
protected:int b;
};
/****************子类继承父类********************/
class Inherit:public Base //继承方式:public
{
public:void test(){b = 10; //父类protected,类内可以访问c = 16; //父类public,类内类外都可以访问int e;}
private:int d;};int main()
{Inherit inherit1;cout<<sizeof(inherit1) <<endl;inherit1.c = 15; //父类public,类内类外都可以访问return 0;
}
父类 | 继承方式 | 子类 |
Public | Protected | Protected(类内可以访问) |
Protected | Protected(类内可以访问) | |
Private | 不可访问 |
#include<iostream>
using namespace std;
/****************父类********************/
class Base
{
public:int c;
private:char a;
protected:int b;
};
/****************子类继承父类********************/
class Inherit :protected Base //继承方式:protected
{
public:void test(){b = 10; //父类protected,类内可以访问c = 16; //父类public,类内可以访问int e;}
private:int d;};int main()
{Inherit inherit1;cout << sizeof(inherit1) << endl;return 0;
}
父类 | 继承方式 | 子类 |
Public | Private | Private(类内可以访问) |
Protected | Private(类内可以访问) | |
Private | 不可访问 |
#include<iostream>
using namespace std;
/****************父类********************/
class Base
{
public:int c;
private:char a;
protected:int b;
};
/****************子类继承父类********************/
class Inherit :private Base //继承方式:private
{
public:void test(){b = 10; //父类protected,类内可以访问c = 16; //父类public,类内可以访问int e;}
private:int d;
};int main()
{Inherit inherit1;cout << sizeof(inherit1) << endl;return 0;
}
规律:
- 继承关系中,不论是哪种继承关系,只要是父类的private的,子类都是不可访问的;
- 继承之后,子类的权限跟继承方式有关,跟父类的权限无关;
- 如果只衍生一代,private和pritected的继承方式类似,子类都只能在类内访问;
如果衍生两代的话,继承方式private只能衍生一代,protected可以继续衍生
#include<iostream>
using namespace std;
/****************父类********************/
class Base
{
public:int c;
private:char a;
protected:int b;
};
/****************子类继承父类********************/
class Inherit :private Base //继承方式:private
{
public:void test(){b = 10; //父类protected,类内可以访问c = 16; //父类public,类内可以访问}
private:int d;
};
/****************子子类继承子类********************/
class AA:public Inherit//由于子类继承方式是private,所以子子类无法再继承
{
void test1()
{}
public:int e;
private:int f;
protected:int g;
};int main()
{Inherit inherit1;cout << sizeof(inherit1) << endl;return 0;
}
三、子类构造函数
子类实例化对象:父类构造 子类构造
子类对象生命周期结束:子类析构 父类析构
1、Person.h
#ifndef _PERSON_H_
#define _PERSON_H_#include<iostream>
using namespace std;
#include<string>
class Person
{
public:Person();Person(string _name);Person(string _name,int _age,string _sex="女");~Person();void eat();void sleep();
protected:string name;int age;string sex;
};
#endif
2、Person.cpp
每个函数名前需要加上类的作用域
#include "Person.h"Person::Person()
{cout << "Person()" << endl;
}
Person::Person(string _name):name(_name)
{cout << "Person(string)" << endl;
}
Person::Person(string _name, int _age, string _sex):name(_name),age(_age),sex(_sex)
{cout << "Person(string,int,string)" << endl;
}
Person::~Person()
{cout << "~Person()" << endl;
}
void Person::eat()
{cout << name << "去吃饭了" << endl;
}
void Person::sleep()
{cout << name << "去睡觉了" << endl;
}
3、Worker.h
#pragma once
#include "Person.h"
class Worker :public Person
{
public:Worker();Worker(string _name);Worker(string _name,int _age,string _sex,int _salary);void work();void show();~Worker();
protected:int salary;};
4、Worker.cpp
#include "Worker.h"Worker::Worker()
{cout << "Worker()" << endl;
}
Worker::Worker(string _name):Person(_name)
{cout << "Worker(string)" << endl;
}
void Worker::work()
{cout <<name<< "在工作" << endl;
}Worker::Worker(string _name, int _age, string _sex, int _salary):Person(_name,_age,_sex),salary(_salary)
{cout << "Worker(string,int,string,int)" << endl;
}
void Worker::show()
{cout << "name="<<name << endl;cout << "age=" << age << endl;cout << "sex=" << sex << endl;cout << "salary=" << salary << endl;
}Worker::~Worker()
{cout << "~Worker()" << endl;
}
5、main.cpp
#include"Worker.h"
#include <iostream>
using namespace std;int main()
{Worker worker("Liy",12,"男",17222);worker.show();return 0;
}
运行结果:
四、继承的种类
1、多层继承
特点:从父类到子类越来越具体,最终实例化的时候只需要实例化子类的最后一个对象即可
1、Person.h
#ifndef _PERSON_H_
#define _PERSON_H_#include<iostream>
using namespace std;
#include<string>
class Person
{
public:Person();Person(string _name);Person(string _name,int _age,string _sex="女");~Person();void eat();void sleep();void setName(string _name);void setAge(int _age);void setSex(string _age);
protected:string name;int age;string sex;
};
#endif
2、Worker.h
#pragma once
#include "Person.h"
class Worker :public Person
{
public:Worker();Worker(string _name);Worker(string _name,int _age,string _sex,int _salary);void work();void show();~Worker();void setSalary(int _salary);
protected:int salary;};
3、Programmer.h
#pragma once
#include "Worker.h"
class Programmer : public Worker
{
public:Programmer();Programmer(string _name,string _level);void coding();~Programmer();void setLevel(string _level);protected:string level;
};
4、Person.cpp
#include "Person.h"Person::Person()
{cout << "Person()" << endl;
}
Person::Person(string _name):name(_name)
{cout << "Person(string)" << endl;
}
Person::Person(string _name, int _age, string _sex):name(_name),age(_age),sex(_sex)
{cout << "Person(string,int,string)" << endl;
}
Person::~Person()
{cout << "~Person()" << endl;
}
void Person::eat()
{cout << name << "去吃饭了" << endl;
}
void Person::sleep()
{cout << name << "去睡觉了" << endl;
}
void Person::setName(string _name)
{}
void Person::setAge(int _age)
{}
void Person::setSex(string _age)
{}
5、Worker.cpp
#include "Worker.h"Worker::Worker()
{cout << "Worker()" << endl;
}
Worker::Worker(string _name):Person(_name)
{cout << "Worker(string)" << endl;
}
void Worker::work()
{cout <<name<< "在工作" << endl;
}Worker::Worker(string _name, int _age, string _sex, int _salary):Person(_name,_age,_sex),salary(_salary)
{cout << "Worker(string,int,string,int)" << endl;
}
void Worker::show()
{cout << "name="<<name << endl;cout << "age=" << age << endl;cout << "sex=" << sex << endl;cout << "salary=" << salary << endl;
}Worker::~Worker()
{cout << "~Worker()" << endl;
}void Worker::setSalary(int _salary)
{}
6、 Programmer.cpp
#include "Programmer.h"
Programmer::Programmer()
{cout << "Programmer()" << endl;
}
Programmer::Programmer(string _name, string _level):Worker(_name),level(_level)
{cout << "Programmer(string,string)" << endl;
}
void Programmer::coding()
{cout <<level<< "工程师" << name<<"在敲代码"<<endl;
}
Programmer::~Programmer()
{cout << "~Programmer()" << endl;
}void Programmer::setLevel(string _level)
{}
2、多重继承(菱形继承)