CPP——继承

认识继承

当上一个打代码的人走后,下一个人不可能从头开始打代码,那怎么办呢?

用到的方法叫继承

作用是继承他的代码并且添加自己的代码进去

#include<iostream>
using namespace std;

class student{   
    //private:
    protected://Student的私有数据无法在子类访问,故引入一个新的关键字protected。意思为子类可访问私密成员,但是其他人不可以
    	string name;
       	int num;
    public:
    student();
    student(char *, char *);
    ~student();
   void print()
    {
        cout << name << num << endl;
    }
};

//新定义的类叫做子类/派生类,原有的类叫做父类/基类
class IOTstudent : public student//此格式为继承,该类继承了student类的所有属性,并且添加了以下自己的属性,成员包含了int类型的cpp和public类型的Student。绝大多数情况是public继承,也可不写(即private方式):此时父类内所有成员变成私密的,父类内的public也是,也可protected
{
private:
    int cpp;
public:
   IOTstudent(){}
   IOTstudent(string name, int num1, int num2)
       :Student(name, num1)
   {
           cpp = num2;
   }
    void print()
    {
        cout << name << num << cpp << endl;//将成员设置成protected,便可在子类内访问,如果是private便不可访问
    }
};

student::student(char *n1, int num1)
{
    ………………;
}

void f(student* p)
{
    p->print();
}

int main()
{
    student s1("zhang", 20230001), *p1;
    IOTstudent s2("li", 20230002, 86), *p2;
    s2.print();//当父类子类定义的函数名字一样时,采用就近原则,系统会使用子类内的函数
    s2.student::print();//若一定要用父类内的函数,则加入::表示在student内的print()函数
    
    s1 = s2;//正确的,因为s2空间较大,可以把s1内的内容赋值到s2,类内不会有随机数
    s2 = s1;//错误的,因为s1空间较小,不可以把s2内的内容赋值到s1,类内会有随机数
    //但是一般不会用,因为没有实用价值,该原理一般用于指针
    
    p1 = &s1;
    p2 = &s2;
    p1->print();
    
    p1 = &s2;//正确的,让父类的指针指向子类的地址,原理如上,指针内不会有随机数
    p2 = &s1;//错误的,原理如上,指针内会有随机数
    //一般会大量使用父类的指针,因为不会出错,无论是赋值自己还是子类
    p1->print();//此时的print是父类的,因为p1的类型是父类的类型
   
    f(&s1);
    f(&s2);
    //两个都是调用父类的print
    
    return 0;
}

注:在构造时,父类先被系统会自动调用构造,子类再调用构造函数;再析构时,子类先析构,父类再被系统自动调用析构函数

多重继承

一个子类继承两个或多个父类/基类

#include <iostream>
using namespace std;

class Person
{
protected:
	string name;
	Person(string n) {name = n;}
}

class Student : virtual public Person // 虚继承
{
protected:
	int id;
public:
	// 如果提供了有参构造函数,系统不会提供缺省构造函数,如果没有提供有参构造函数,则系统会提供一个缺省的构造函数
	Student() : Person("") {} // 派生类必须调用父类的构造函数
	Student(string n, int i) : Person(n), id(i) {cout << "CS\n";} // 标准格式,派生类必须调用父类的构造函数
	~Student() {cout << "DS\n";}
	void print() {cout << name << id;}
};

class Teacher : virtual public Person
{
protected:
	double salary;
public:
	Teacher(string, double) : Person(n){salary = s; cout << "CT\n";} // 不标准
	~Teacher() {cout << "DT\n";}
	void print() {cout << name << salary;}
};

// 先执行Student的构造函数,再执行Teacher的构造函数,先析构Teacher再析构Student,顺序由此决定
class SJ : public Student, public Teacher // 记得加上public,否则默认private
{
public:
	SJ(string, int, double);
	~SJ() {cout << "Dsj\n";}
	void print();
};

SJ::SJ(string n, int i, double s) // 要给父类提供参数,用标准格式初始化
	: Persong(n), // 必须给祖先类实现构造函数
	: Student(n, i), Teacher(n, s) // 如果不写Student(n, i),则会调用Student的无参构造函数
{
	cout << "Csj\n";
}

void SJ::print()
{
	cout << name << id << name2 << salary;

	print1(); // 结果和上面相同
	print2();

	// 或者预操作
	Student::print();
	Teacher::print();

	cout << name << id << salary; // error,因为有两个类,但是在类处加了virtual之后便可以通过,因为系统默认只有一个

	// 使用预操作
	cout << Teacher::name << id << salary; // 对

	cout << name << id << salary; // 对
}

int main()
{
	// Student s;
	// Teacher t("zhang", 20000.0);
	SJ sj("li", 20230001, 4000.0);

	return 0;
}

运算符重载关键字operator

  • 在有参构造函数的参数加等于号有什么意义,代替什么?
  • 在函数内怎么返回它本身呢?
  • operator的使用形式?
  • 如何输出一个类?(输入呢?)
  • 成员函数内之所以可以访问自身,是因为它有this指针,但是友元函数没有,所以要手动加入,一般为第一个形参。
  • 一般重载操作符左边数据类型是什么? 答:定义在哪个类便是什么
#include<iostream>
uisng namespace std;

class Complex
{
private:
	double x, y;
public:
    Complex(double xx = 0, double yy = 0){ x = xx; y = yy; }//当参数只传进来一个值或没有参数时,不会初始化x或y。所以在参数处赋值,假设没有传入相应参数也会自动初始化,便不需要补充缺省函数也不会报错。
    void print()
    {
        cout << x;
        if(y > 0) cout << "+" << y << 'i';
        if(y < 0) cout << y << 'i';
	}
    Complex& add(const Complex& c)
    {
        x += c.x;
        y += c.y;
        return *this;//返回自己出去,可以这样子写,小技巧
	}
    Complex& operator+= (const Complex& c)//操作符重载函数,运用operator关键字重新赋以操作符功能以减少我们main的代码量,实现与add函数相同的功能
    {
        x += c.x;
        y += c.y;
        return *this;
    }
    Complex operator+ (const Complex& c)//想实现c=c1+c2,因为不修改c1只是想让c1+c2,所以不适用&,而是选择返回一个类
    {
        Complex temp;
        temp.x = c.x + x;
        temp.y = c.y + y;
        return temp;
    }
    friend ostream operator<< (ostream&, Complex);
    friend ostream operator>> (istream&, Complex);//重载>>,输入
};

ostream operator<< (ostream& o, const Complex& c)//有了这个代码,你便可以直接输出一个类
{
    o << c.x;//ostream为cout的类型,此处定义的o代替了cout
    if(c.y > 0) o << "+" << c.y << 'i';
    if(c.y < 0) o << "-" << c.y << 'i';
    return o;
}

int main()
{
    Complex c1;
    Complex c2(1.1, -2.2);
    Complex c3(1.3, 4);
    c1.print(); cout << endl;
    c2.print(); cout << endl;
    c3.print(); cout << endl;
    
    c1.add(c2);//add()函数
    c1 += c2;//运算符重载函数
    cout << c1;//在没有添加void operator<<函数时,这样子会报错,那如何可以直接输出一个类呢,答案便是重载‘<<’,但是得用友元函数,不能用成员函数了
    
    return 0;
}

注:

  • 不可重载的运算符
..*::?:sizeof
  • 若使用=这个运算符,当类内有new我们需要自己重新重载一下,但是如果没有,不重载也可以进行c1 = c3 + c2的操作
  • operator int(){},这叫做重载类型转换运算符,这样子使用‘(int)变量名称’,其他数据类型也一样,其本身技术函数返回类型,故不用在operator前再加返回类型
  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值