C++面向对象 class five

  • 不同继承方式的效果;
  • 构造函数的调用顺序;
  • 子类的拷贝构造函数;
  • 通过子类调用父类的函数;
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cstdlib>
using namespace std;
class Time{
public:
    Time(){cout<<"Time is constructor."<<endl;}
    ~Time(){cout<<"Time is constructor."<<endl;}
};
class Date{
public:
    Date(int y,int m,int d){
        year = y;
        month = m;
        day = d;
        cout<<"Date's constructor."<<endl;
    }
    ~Date(){
        cout<<"Date's de-constructor."<<endl;
    }
private:
    int year,month,day;
};
class Person{
	public:
		Person(int _age){
		    age = _age;
		    cout<<"Person's constructor."<<endl;
		}
		~Person(){
		    cout<<"Person's de-constructor."<<endl;
		}
	protected://只放开给子类,不给组合类开放
		int age;
		Time time;
};
class Student:public Person,public Time{
    public:
        Student(int _age,string _id,int y,int m,int d):birthday(y,m,d),Person(_age){//构造函数调用顺序:先父再组合再子
            id = _id;
            cout<<"Student's constructor."<<endl;
        }
        ~Student(){
            cout<<"Student's de-constructor."<<endl;
        }
        void show(){
            cout<<age<<' '<<id<<endl;
        }
    private:
        string id;
        Date birthday;
//        Time time;
};
int main()
{
    Student a(18,"1060305",2002,5,22);
    a.show();
    return 0;
}

#include<iostream>
#include<cstring>
#include<iomanip>
#include<cstdlib>
using namespace std;
class Date{
public:
	Date(int y = 0,int m = 0,int d = 0){
		year = y;
		month = m;
		day = d;
		cout<<"Date is constructor."<<endl;
	}
	~Date(){
		cout<<"Date is de-constructor."<<endl;
	}
private:
    int year,month,day;	
};
class Vehicle{
	public:
		Vehicle(int _speed = 0){
			speed = _speed;
			cout<<"Vehicle is default."<<endl;
		}
		Vehicle(const Vehicle &another){
			speed = another.speed;
			cout<<"Vehicle is copyed."<<endl;
		}
		~Vehicle(){
			cout<<"Vehicle is de-constructor."<<endl;
		}
		void show()
		{
			cout<<speed<<endl;
		}
	protected:
		int speed;
		static int cnt;
}; 
class Car:public Vehicle{
	public:
		Car(int speed = 0,int y = 0,int m = 0,int d = 0,string b = " "):Vehicle(speed),produceDate(y,m,d){
			brand = b;
			cout<<"Car is constructor."<<endl;
		}
		Car(const Car &another):Vehicle(another){//拷贝构造函数 
			brand = another.brand;
			cout<<"Car is copied."<<endl;
		}
		~Car(){
			cout<<"Car is de-constructor."<<endl;
		}
//		using Vehicle::speed;
	private:
		string brand;
	//	using Vehicle::speed;
		Date produceDate;
	protected:
		
};
//class My_Car:public Car{
//	public:
//		My_Car(){
//			cout<<"My_Car is constructor."<<endl;
//		}
//		void show()
//		{
//			cout<<speed<<endl;
//		}
//};
int Vehicle::cnt = 0;
int main()
{
	Car x(80,2002,5,22,"baoma");
	cout<<endl;
	Car y(x);
	y.Vehicle::show();//通过子类调用父类的方法 
//	My_Car a;
//	a.speed = 1;
//	cout<<a.speed<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈希表扁豆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值