Case:
Application of Copy Constructor
Create a Teacher class, including private integer id, string type name, and
public member methods including no-argument constructor,
full-argument constructor, copy constructor, and
copy constructor implementation: name+"Mr."
input:
none
Output:
1 Mr. Li Qiang
#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;
}
Case:
Destructor
Define a Car class, which includes the following content in the class definition:
private data members include
string brand
double price
public member functions include
no-argument constructor:
full-argument constructor:
display() function: output brand price
analysis Constructor: Output "Destructor called"
input:
none
Output:
Audi 230000
calls the destructor
BMW 350000
calls the destructor
Mercedes Benz 400000
calls the destructor
#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;
}
Case:
comprehensive application
Define a Book class, which includes the following content in the class definition:
private data member:
string bookname
double price
int number
public member function:
no parameter constructor: realize the initial value book name = "C language", price = 33.5, Quantity = 10000
Full parameter constructor:
copy constructor: implement object value copy, and output "calling the copy constructor"
display() function: output book title price quantity
borrow() function: reduce the current quantity by 1
restore() Function: Increment the current amount by 1
Destructor: Output "Destructor called"
input:
none
Output:
Call copy constructor
C language 33.5 10000
Java language 48.5 5000
Java language 48.5 4999
Call destructor
Call destructor Call
destructor
#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;
}
Homework solved~