编程语言
首页 > 编程语言> > C++——掌握类构造函数与析构函数的使用方法(二)

C++——掌握类构造函数与析构函数的使用方法(二)

作者:互联网

1.分别使用无参和有参的构造函数给成员变量赋值(row=10,col=10),并输出。

代码:

#include<iostream>
using namespace std;
class Table
{
public:
	Table(){row = 10; col = 10;}
	Table(int a, int b):row(a), col(b){};
	~Table(){};
	void show();
private:
	int row;
	int col;
};
void Table::show()
{
	cout << "row:" << row << endl;
	cout << "col:" << col << endl;
}
int main()
{
	Table t1;
	Table t2(10, 10);
	t1.show();
	t2.show();
	return 0;
}

结果:
image.png

2.使用默认参数的构造函数给成员变量赋值,并输出。

代码:

#include <iostream>
using namespace std;
class SDate
{
private:
	int year;
	int month;
	int day;
public:
	SDate(int y,int m,int d);
	void outShow()
	{
		cout<<"year:"<<year<<endl;
		cout<<"month:"<<month<<endl;
		cout<<"day:"<<day<<endl;
	}
	~SDate(){cout<<year<<month<<day<<endl;}
};
SDate::SDate(int d,int m=4,int y=2021)
{
	year=y;
	month=m;
	day=d;
}
int main()
{
	SDate s1(20);
	s1.outShow();
	SDate s2(5,20);
	s2.outShow();
	return 0;
}

结果:
image.png

3.使用全局对象,查看全局对象的构造顺序

代码:

#include<iostream>
using namespace std;
class SimpleClass
{
public:
	SimpleClass(char *time)
	{
		cout << "Create a SimpleClass!" << time << "\n";
	}
};
SimpleClass s("before main");
int main ()
{
	SimpleClass sm("in main");
	return 0;
}
SimpleClass sa("after main");

结果:
image.png

4.使用静态对象,观察静态对象的构造函数的执行次数。

代码:

#include<iostream>
using namespace std;
class whatYouLike
{
public:
	whatYouLike(char *m)
	{
		strcpy(fruit, m);
		fruit[19] = '\0';
		cout << "I like "<< fruit << "\n";
	}
	char fruit[20];
};
void fn(char *f)
{
	static whatYouLike s(f);
	static int i;
	i++;
	cout << i << " in fn,give you a(/an) " << f << "\n";
	cout << "WhatYouLike is" << s.fruit << endl;
}
int main()
{
	fn("apple");
	fn("orange");
	fn("banana");
	return 0;
}

结果:
image.png

5.动态创建对象,观察构造函数的执行。

代码:

#include<iostream>
using namespace std;
class SClass
{
public:
	SClass()
	{
		cout << "Create a SClass object!" << "\n";
	}
};
int main()
{
	SClass *sq;
	sq = new SClass();
	cout << "Main creates a SClass object in heap!\n";
	delete sq;
}

结果:
image.png

6.了解多个类之间的复用:可将一个类声明为另一个类的成员变量。

代码:

#define N 20
#include<iostream>
#include<string>
using namespace std;
class cupCover
{
public:
	cupCover(char *myColor)
	{
		strcpy(color, myColor);
		color[N-1] = '\0';
		cout << "The cupCover is create! color=" << color << endl;
	}
private:
	char color[N];
};
class cupBody
{
public:
	cupBody()
	{
		cout << "The cupBody is created!\n";
	}
};
class cup
{
public:
	cup(char *colors):cupCover(colors)
	{
		cout << "The cup is created!It has cupCover and cupBody!\n";
	}
private:
	cupCover cupCover;
	cupBody cupBody;
};
int main()
{
	cup littleCup("black");
	return 0;
}

结果:
image.png

7.定义员工类Employee,员工信息有:编号,姓名,部门,成员函数有输出员工信息,修改员工信息(修改单个信息)。要求成员变量为私有的,修改员工信息的函数为保护的。设计类,使用类,并修改员工信息。(包括构造函数和析构函数)

代码:

#include<iostream>
#include<string>
using namespace std;
class Employee
{
private:
	int number;
	string name;
	string department;
public:
	Employee(int num,string n,string d)
	{
		number=num;name=n;department=d;
	}
	void change1(int c1,string c2,string c3)
	{
		change2(c1,c2,c3);
	}
	~Employee()
	{
		cout<<"修改成功"<<endl;
	}
	void print()
	{
		cout<<"number:"<<number<<" name:"<<name<<" department:"<<department<<endl;
	}
protected:
	void change2(int c1,string c2,string c3)
	{
		number=c1,name=c2,department=c3;
	}
};
int main()
{
	int a,i;
	string b,c,j,k;
	cout<<"please input the employee's number,name and department:";
	cin>>a>>b>>c;
	Employee a1(a,b,c);
	cout<<"当前员工信息为:"<<endl;
	a1.print();
	cout<<"需要修改,请输入你要修改的编号,性名,部门:";
	cin>>i>>j>>k;
	a1.change1(i,j,k);
	a1.print();
	return 0;
}

结果:
image.png

8.定义MyDesk类,长和宽为私有成员变量。要求使用构造函数重载,实现对长和宽的赋值并输出。构造函数分别为无参数、一个参数和两个参数。设计类,并使用类。

代码:

#include <iostream>
using namespace std;
class MyDesk
{
private:
	int long0;
	int wide;
public:
	MyDesk()
	{
		long0=3,wide=2;
	}
	MyDesk(int x):long0(x)
	{
		wide=3;
	}
	MyDesk(int x,int y):long0(x),wide(y)
	{
	}
	void print()
	{
		cout<<long0<<" and "<<wide<<endl;
	}
};
int main()
{
	MyDesk my1;
	cout<<"无参数的构造函数的长宽为"<<endl;
	my1.print();
	MyDesk my2(3);
	cout<<"一个参数的构造函数的长宽为"<<endl;
	my2.print();
	MyDesk my3(4,5);
	cout<<"两个参数的构造函数的长宽为"<<endl;
	my3.print();
	return 0;
}

结果:
image.png

标签:cout,int,与析构,C++,using,include,public,构造函数
来源: https://blog.csdn.net/weixin_50464560/article/details/116614227