C++学习:第三章C++语言基础 - (九)运算符重载、继承

new delete 关键字重载

#include <iostream>

using namespace std;

char mem[10000];
int pos=0;

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

	static void* operator new(size_t bytes){
		cout<<"new A"<<bytes<<endl;
		int alloc = pos;
		pos += bytes;
		return mem+alloc;
	}

	static void operator delete(void* p){
		cout<<"delete A"<<endl;
		//回收这里省略未写
	}

	static void* operator new[](size_t bytes){
		cout<<"new[] A"<<bytes<<endl;
		int alloc = pos;
		pos += bytes;
		return mem+alloc;
	}

	static void operator delete[](void* p){
		cout<<"delete[] A"<<endl;
	}

};

void* operator new(size_t bytes){
	cout<<"new "<<bytes<<endl;return mem;
}

void operator delete(void* p){
	cout<<"delete "<<endl;
}

int main()
{
	A* x = new A;	//1、分配空间;2、执行构造函数
	delete x;		//1、执行析构函数;2、释放空间
	x = new A[3];
	delete[] x;
	int* p = new int;
	delete p;
}

 

继承:

class 子类:public 父类1, public 父类2{

}

public     公开继承:所有人可以访问父类内容
protected  保护继承:本类及其子类可以访问父类内容
private    私有继承:只有本类可以访问父类内容
#include <iostream>
#include <string>

using namespace std;

class Person{
    string name;
    bool   gender;

protected:
    int height;

public:
    void eat(const char* food){
        cout << name << " is eating " << food << endl; 
    }

    void sleep();

    void show(){
        cout << "my name is " << name << ", gender is "
        << (gender?"man , height is " : "woman , height is ") 
        << height << " . " << endl;
    }

    Person(const char* name, bool gender, int height):
        name(name),gender(gender),height(height)
    {}

    const string& Name()const{
        return name;
    }
};

class Teacher : public Person {
    string course;

public:
    void teach(const char* class_){
        height = 999;
        cout << Name() << " teacher is teaching " <<
            course << " in class " << class_ <<
             " and height is " << height << endl; 
    }

    //用父类的初始化函数可以初始化父类的私有变量
    Teacher(const char* name, bool gender, int height, const char* course)
        :Person(name, gender, height), course(course) {
        cout << "height now is " << height << endl; 
    }

    //虽然继承的父类有该方法,但是可以同名、同传参重写
    //系统会自动隐藏父类的该方法。
    //注意:即使是不同参数表也会隐藏主类的同名方法
    //为什么不是重载:因为他们不是同一个层次,不存在重载
    void show(){
        cout << "rewrite" << endl; }
    };

int main(){

    Person a("Thomas", true, 180);
    a.show();
    a.eat("fish");

    Teacher t("Tom", false, 175, "English");
    t.teach("1502");
    t.show();

    getchar();
    return 0;
}
#include <iostream>
#include <string>

using namespace std;

class Person{
protected:
	string name;

private:
	bool gender;

public:
	void eat(const char* food){
        cout<<name<<"在吃"<<food<<endl;
    }
	void sleep();
	void show(){
		cout << "大家好,我是" << (gender?"帅哥":"美女") 
        << name << ",很荣幸认识你!" << endl;
	}
	Person(const char* name, bool gender)
	    :name(name),gender(gender){}
	    string Name()const{return name;
    }
};


class Teacher : public Person{
	string course;

public:
	void teach(const char* _class){
		cout << name << "老师在给" << _class << "班讲" << course << "课程" << endl;
	}

	Teacher(const char* name, bool gender, const char* course)
	    :Person(name,gender),course(course){}
	void show(const char* _class){//改写来自父类的成员,隐藏来自父类那个同名成员
		cout << _class << "班的" << "同学们好,我是" 
        << course << "老师" << name << ",希望能带领大家把" 
        << course << "课程学好!(掌声)\n";
	}
};


int main()
{
	Person a("芙蓉",false);
	Teacher b("杨强",true,"UNIX C");
	a.eat("巧克力");
	b.eat("泡椒凤爪");
	a.show();
	b.show("csd1007");
	b.Person::show();
	b.teach("csd1007");
//	cout << b.name << endl;保护成员不能在外界访问
//	cout << a.name << endl;保护成员不能在外界访问
	cout << a.Name() << endl;
	cout << b.Name() << endl;
}

在同一继承体系中:

upcast(向上转换即子类转成父类):没有问题.因为父类的行为都包含在子类中;

downcast(向下转换):有可能会出现问题,编译时可能不会发现.

一个类的行为和自身的类型相关.也就是一个A类型的指针总会优先调用自己A类内的函数,当然发生继承中的重写(虚继承等)例外.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值