【C++学习笔记5】实验5-类与对象基本知识(2)

案例:
拷贝构造函数应用

创建一个Teacher类,包含私有的整型id,string类型的name,
公有的成员方法包含无参构造函数,
全参构造函数,拷贝构造函数,
拷贝构造函数实现:name+“先生”。

输入:

输出:
1 李强先生

#include<iostream>
#include<string>
using namespace std;


class Teacher{
    private:
        int id;
        string name;
    public:
        Teacher();
        Teacher(int,string);
        Teacher(const Teacher &data);//拷贝构造函数声明
        void disPlay();
};

//在外面写要 类名::构造函数名(){}
//构造函数,为数据成员赋值
//构造函数的函数名必须和类名一致
//构造函数没有返回值,不用加,void、int、char什么的
//构造函数为public属性,定义对象自动调用
//构造函数仅在创建对象时系统自动调用

Teacher::Teacher(){//无参构造

}

Teacher::Teacher(int idd,string namee){//全参构造
    id=idd;
    name=namee;
}

Teacher::Teacher(const Teacher &data){//拷贝构造函数定义
    id=data.id;
    name=data.name;
}

void Teacher::disPlay(){//打印输出
    cout<<id<<" "<<name<<"先生"<<endl;
}


int main(){
	Teacher teacher1(1,"李强");
	Teacher teacher2(teacher1);
	teacher2.disPlay();
	return 0;

}

案例:
析构函数

定义一个Car类,在该类定义中包括如下内容:
私有数据成员包括
string brand
double price
公有成员函数包括
无参构造函数:
全参构造函数:
display()函数:输出 品牌 价格
析构函数:输出 “调用析构函数”

输入:

输出:
奥迪 230000
调用析构函数
宝马 350000
调用析构函数
奔驰 400000
调用析构函数

#include <iostream>
#include<string>
using namespace std;


class Car{
    private:
        string brand;//品牌
        double price;//价格
    public:
        Car();
        Car(string,double);
        ~Car();//析构函数
        void display();
};
Car::Car(){

}
Car::Car(string brand1,double price1){
    brand=brand1;
    price=price1;
}
//当对象生命周期结束时,需要释放所占内存资源,程序将自动调用类的析构函数来完成
//1.析构函数也是类的成员函数,函数名和类名相同,类名前要加“~”
//2.析构函数没有返回类型,必须定义为共有成员函数
//3.析构函数没有参数,也不能重载,每个类有且仅有一个
//4.析构函数由系统自动调用:一是,对象生命周期结束时调用,二是,用new动态创建的对象,用delete释放申请内存时也会自动调用
Car::~Car(){
    cout<<"调用析构函数"<<endl;
}
void Car::display(){
    cout<<brand<<" "<<price<<endl;
}



int main()
{ 
	//情况1:delete释放资源时会调用析构函数
	Car *car1 = new Car("奥迪",230000);
	car1->display();
	delete car1;
	//情况2:匿名对象调用析构函数
	Car("宝马",350000).display();
	//情况3:return 0 会调用析构函数
	Car car2("奔驰",400000);
	car2.display();
    return 0;
}

案例:
综合应用

定义一个Book类,在该类定义中包括如下内容:
私有数据成员:
string bookname
double price
int number
公有成员函数:
无参构造函数:实现初始值书名=“C语言”,价格=33.5,数量=10000
全参构造函数:
拷贝构造函数:实现对象值拷贝,并输出“调用拷贝构造函数”
display()函数:输出 书名 价格 数量
borrow()函数:将当前数量减1
restore()函数:将当前数量加1
析构函数:输出 “调用析构函数”

输入:

输出:
调用拷贝构造函数
C语言 33.5 10000
Java语言 48.5 5000
Java语言 48.5 4999
调用析构函数
调用析构函数
调用析构函数

#include <iostream>
#include<string>
using namespace std;



class Book{
    private:
        string bookname;//书名
        double price;//价格
        int number;//数量
    public:
        Book();
        Book(string,double,int);
        Book(const Book &goodbook);
        ~Book();
        void display();
        void borrow();
        void restore();
};
Book::Book(){//无参构造,实现初始值,书名="C语言",价格=33.5,数量=10000
    bookname="C语言";
    price=33.5;
    number=10000;
}
Book::Book(string bookname1,double price1,int number1){//全参构造
    bookname=bookname1;
    price=price1;
    number=number1;
}
Book::Book(const Book &goodbook){//实现对象值拷贝
    bookname=goodbook.bookname;
    price=goodbook.price;
    number=goodbook.number;
    cout<<"调用拷贝构造函数"<<endl;
}
Book::~Book(){
    cout<<"调用析构函数"<<endl;
}
void Book::display(){//打印输出
    cout<<bookname<<" "<<price<<" "<<number<<endl;
}
void Book::borrow(){//当前数量减1
    number--;
}
void Book::restore(){///当前数量加1
    number++;
}



int main()
{ 
	Book book1;
	Book book2("Java语言",48.5,5000);
	Book book3(book2);
	book1.display();
	book2.display();
	book3.borrow();
	book3.display();	
    return 0;
}

作业解决~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值