C++_第二次实验:常成员、静态成员、this指针(待补充返回值)、构造函数、拷贝构造函数(带析构函数的)、指针相互赋值

题目:常数据成员&常成员函数

在这里插入图片描述

#include<iostream>    
using namespace std;
class CTest{
	public: 
		const int y2;  
		CTest (int i1,int i2):y1(i1),y2(i2) 
		{ 
//			y1=10;	//常数据成员进行初始化,只能在构造函数的初始化列表进行,不能在构造函数内部进行 
		  	x=y1;
		}
		int readme() const;
		   //...
	private:
		int x;
		const int y1;
};
int CTest::readme() const
{ 
	int i;
	i=x;
//	x++;	//常成员函数可以访问不可改变数据成员
	return x;
}
int main()
{ 
	CTest c(2,8);
	int i=c.y2;
//	c.y2=i;	//常数据成员一旦定义不能更改 
//	i=c.y1;	//类的private数据成员在类外不能直接访问 
	cout<<i<<endl;
	return 0;
}

错误1. CTest类的构造函数中给常数据成员y1赋值。常数据成员进行初始化,只能在构造函数的初始化列表进行,不能在构造函数内部进行。
错误2. 常成员函数中修改了数据成员x的值。常成员函数可以访问但不可改变数据成员。
错误3. main函数中修改了常数据成员的值。常数据成员一旦定义不能更改。
错误4. main函数中直接访问私有成员y1。类的private数据成员在类外不能直接访问。

题目:this指针

在这里插入图片描述

#include<iostream>    
using namespace std;
class CTest{
	public:
	    CTest ()
	    { 
			x=20;
	    }
	    void use_this();
	private:
	    int x;
};
void CTest::use_this()
{ 
	CTest y,*pointer;
//	this=&y;	//this指针不能直接赋值 
	(*this).x=10;	//括号 
	pointer=this;
	pointer=&y;
}
int main()
{ 
	CTest y;
//	this->x=235;	//this指针只能在类的成员函数里调用
	return 0;
}

错误1. 类的use_this()成员函数中,this指针赋值。this指针不能直接赋值。
错误2. 类的use_this()成员函数中,this指针用法错误。应该加上括号:(*this).x=10;
错误3. main函数中,调用了this指针。this指针只能在类的成员函数里调用

题目:构造函数、构造函数与new运算符、字符指针

在这里插入图片描述

#include <iostream>
using namespace std;
class Cylinder {
	private:
		double radius,height;	//圆柱体的半径,高度
		double volume = 0;	//体积
	public:
		Cylinder(double a, double b):radius(a), height(b)
		{
			
		}
		void printVolume();
};
void Cylinder::printVolume()
{
	volume = 3.14*radius*radius*height;
	cout<<"Volume is:"<<volume<<endl;
}
int main()
{
	double x,y;
	cout<<"输入半径和高度:"<<endl;
	cin>>x>>y;
	Cylinder A(x,y);
	A.printVolume();
	return 0;
}

可以正常运行,有警告:
在这里插入图片描述
在这里插入图片描述
变成这样就没有警告了。数据成员在成员函数内赋值,而不能在类内定义时赋值!!
(其实没必要赋初值

#include <iostream>
using namespace std;
class Cylinder {
	private:
		double radius;
		double height;	//圆柱体的半径,高度
		double volume;	//体积
	public:
		Cylinder(double a, double b):radius(a), height(b)
		{
			
		}
		void printVolume();
};
void Cylinder::printVolume()
{
	volume = 0.0;
	volume = 3.14*radius*radius*height;
	cout<<"Volume is:"<<volume<<endl;
}
int main()
{
	double x,y;
	cout<<"输入半径和高度:"<<endl;
	cin>>x>>y;
	Cylinder A(x,y);
	A.printVolume();
	return 0;
}
题目:

在这里插入图片描述

#include <iostream>
using namespace std;
class Book {
	private:
		string bookname;	//书名
		int price;	//价格
		int number;	//存书数量
	public:
		Book(string a, int b, int c):bookname(a), price(b), number(c)
		{
			
		}
		void display();	//显示图书的情况
		void borrow();	//将存书数量减1。并显示当前存书数量
		void restore();	//将存书数量加1,并显示当前存书数量
};
void Book::display()
{
	cout<<"书名:"<<bookname<<endl<<"价格:"<<price<<endl<<"存书数量:"<<number<<endl; 
}
void Book::borrow()
{
	number--;
	cout<<"存书数量:"<<number<<endl; 
}
void Book::restore()
{
	number++;
	cout<<"存书数量:"<<number<<endl; 
}
int main()
{  
	Book b1("English",20,3), b2("math",24,5);
	b1.display();
	b1.borrow();
	b2.display();
	b2.restore();
	return 0;
}
题目:拷贝构造函数、字符指针

在这里插入图片描述
答案:
c++类与对象实现字符串相加(深拷贝函数)(定义一个MyString类,实现两个字符串相加。)

#include <iostream>
#include <cstring>
using namespace std;
class MyString {
	private:
		char *str;
	public:
		MyString()	// 无参构造函数,因为有 MyString str4;
		{
			str = new char[10];
			strcpy(str, "默认");
		}
		MyString(const char *mystr) {	// 自定义有参构造函数 
			str = new char[strlen(mystr)+1];	// 开辟一个数组 
			strcpy(str, mystr);	// strcpy连‘\0’一起拷贝了 
		} 
		MyString(const MyString &p) {	// 拷贝构造函数 
			str = new char[strlen(p.str)+1];
			strcpy(str, p.str); 
		}
		void display();
		void MyStrcat(MyString a);
};
void MyString::display()
{
	cout<<str<<endl;
}
void MyString::MyStrcat(MyString a)
{
	char *p = new char[strlen(str)+1];	// 把str存起来 
	//p = str;
	strcpy(p, str);
	str = new char[strlen(str)+strlen(a.str)+1];
	strcpy(str, p);
	strcat(str, a.str);
	delete []p; 
}
int main()
{
    MyString str1("Hello");
    MyString str2("World!");
    MyString str3(str1);
    MyString str4;

    str1.display();
    str2.display();
    str3.display();
    cout<<endl;

    str1.MyStrcat(str2);
    str2.MyStrcat(str1);
    str4.MyStrcat(str1);
    str4.MyStrcat(str2);

    str1.display();
    str2.display();
    str3.display();
    str4.display();
    cout<<endl;

    return 0;
}

1.Main函数中直接定义了一个对象:MyString str4;没有传入参数,说明类中存在一个无参构造函数:
MyString() {
str = new char[10];
strcpy(str, “默认”);
}
2.拷贝构造函数中,为了避免出现指针悬挂,要分配新的空间来存储字符数组:str = new char[strlen(p.str)+1];
3.将字符数组放入新分配的空间时,使用for循环或strcpy函数,不能直接赋值。直接赋值只是将指针指向同一个内存空间:char *p = new char[strlen(str)+1]; strcpy(p, str);

题目:静态成员

在这里插入图片描述

#include <iostream>
using namespace std;
class Temperature {
	private:
		double temp;
	public:
		static double totTemp;
		static double count;
		void setTemp(double a);
		static double Average();
};
double Temperature::totTemp = 0.0;
double Temperature::count = 0;
void Temperature::setTemp(double a)
{
	totTemp += a;
	Temperature::count += 1;
}
double Temperature::Average()
{
	return totTemp/count;
}
int main()
{   
	Temperature t[10];
	double temp;
	int i;
	for(i=0;i<7;i++)
	{   
		cout<<"输入第"<<i+1<<"天的温度:"<<endl;
		cin>>temp;
		t[i].setTemp(temp);
	}
	cout<<"七天的平均温度为:"<<Temperature::Average()<<endl;
	return 0;
}

1.创建静态数据成员在类内:static double totTemp; 初始化静态数据成员在类外:double Temperature::totTemp = 0.0;
2.静态数据成员属于整个类而不是某个对象,调用静态数据成员时要用类名:Temperature::count += 1;

总结:

创建构造函数时要注意main函数中定义对象的形式、有无传参,从而创建有参/无参构造函数。
静态成员在类内定义类外初始化,可以更新,是整个类的而不是某个对象的,使用的时候要注意通过类名而不是对象名。
为了避免指针悬挂问题,自定义拷贝函数也就是深拷贝,注意要用new分配空间。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值