C++笔记

这篇博客探讨了C++中对象的拷贝构造函数、浅拷贝与深拷贝的概念,以及`this`指针和友元函数在内存管理中的应用。通过示例代码详细解释了如何在类设计中处理对象复制和内存分配问题,以确保正确性和效率。

生活嘛~慢慢来,至于明天嘛,我再想想办法!!

#include<iostream>
#include<Cstring>
#include<cstdlib>
using namespace std;
class Student
{
private:
	char name[20];
	int age;
	int num;
public:
	Student(int n, char* str, int s);
	~Student();
	void print();
	void set(int n, char* str, int a);
};

void Student::print()
{
	cout << name << "   " << num << "   " << age << endl;
}

Student::Student(int n, char* str, int a)
{
	num = n;
	strcpy_s(name,str);
	age = a;
}

void Student::set(int n, char* str, int a)
{
	num = n;
	strcpy_s(name, str);
	age = a;
}

Student::~Student()
{
	cout << name << "   " << num << "   " << age << endl;
}


int main()
{
	char name[20] = "lheng0";
	char name1[20] = "lheng1";

	char* p = name;
	Student A(102, p, 21);
	p = name1;
	Student B(102, p, 21);

	//A.set(1002, "liwensheng", 21);
	//A.print();

	return 0;
}

拷贝构造函数

#include<iostream>
using namespace std;
#define PI 3.1415926

class Circle
{
public:
	Circle(double R);
	Circle(Circle &A);
	double area();
	double girth();
private:
	double R;
};

Circle::Circle(double R)
{
	cout << "Constructor" << endl;
	this->R = R;
}

Circle::Circle(Circle& A)
{
	cout << "copy Constructor" << endl;
	this->R = A.R;
}

double Circle::area()
{
	return PI * R * R;
}

double Circle::girth()在这里插入代码片
{
	return PI * 2 * R;
}

int main()
{
	//第一次定义的A对象调用带参数的构造函数
	Circle A(5);
	//第二个B对象由于是通过A对象来初始化
	Circle B(A);
	return 0;

浅拷贝

#include<iostream>
using namespace std;
#define PI 3.1415926

class Circle
{
public:
	Circle(double R,char*str);
	//Circle(Circle &A);
	~Circle();
	double area();
	double girth();
private:
	double R;
	char* str;
};

Circle::Circle(double R, char* str)
{
	cout << "Constructor" << endl;
	this->R = R;
	this->str = new char[strlen(str)+1];
	strcpy(this->str, str);
	cout << this->R << " " << this->str << endl;
}

//Circle::Circle(Circle& A)
//{
//	delete[]str;
//}

Circle::~Circle()
{
	delete[]str;
}

double Circle::area()
{
	return PI * R * R;
}

double Circle::girth()
{
	return PI * 2 * R;
}

int main()
{
	char name[20]= "NO1.Old class";
	//第一次定义的A对象调用带参数的构造函数
	Circle A(5, name);
	//第二个B对象由于是通过A对象来初始化
	Circle B(A);
	return 0;
}

深拷贝


#include<iostream>
using namespace std;
#define PI 3.1415926

class Circle
{
public:
	Circle(double R, char* str);
	Circle(Circle &A);
	~Circle();
	double area();
	double girth();
private:
	double R;
	char* str;
};

Circle::Circle(double R, char* str)
{
	cout << "Constructor" << endl;
	this->R = R;
	this->str = new char[strlen(str) + 1];
	strcpy(this->str, str);
	cout << this->R << " " << this->str << endl;
}

Circle::Circle(Circle& A)
{
	cout << "Copy Constructor" << endl;
	this->R = A.R;
	this->str = new char[strlen(A.str) + 1];
	strcpy(this->str, A.str);
}

Circle::~Circle()
{
	delete[]str;
}

double Circle::area()
{
	return PI * R * R;
}

double Circle::girth()
{
	return PI * 2 * R;
}

int main()
{
	char name[20] = "NO1.Old class";
	//第一次定义的A对象调用带参数的构造函数
	Circle A(5, name);
	//第二个B对象由于是通过A对象来初始化
	Circle B(A);
	return 0;
}

this指针

#include<iostream>
using namespace std;
class Clock
{
public:
	int SetTime(int h, int m, int s);
	void print();
private:
	int H;
	int M;
	int S;
};
int Clock::SetTime(int h, int m, int s)
{
	/*this->H = h;
	this->M = m;
	this->S = s;*/
	(*this).H = h;
	(*this).M = m;
	(*this).S = s;
	return 0;
}
void Clock::print()
{
	cout << this->H << ":" << this->M<< ":"<< this->S;
}
int main()
{
	Clock C;
	C.SetTime(23,59,0); 
	C.print();
	return 0;
}

友元函数friend
求两点距离

#include<iostream>
using namespace std;
class Point
{
private:
	double x;
	double y;
public:
	Point(double a, double b);//构造函数
	friend double Distance(Point &a, Point &b);
};
//构造函数
Point::Point(double a, double b)
{
	this->x = a;
	this->y = b;
}
double Distance(Point& a, Point& b)
{
	double xx = a.x - b.x;
	double yy = a.y - b.y;
	return sqrt(xx* xx+yy*yy);
}
int main()
{	
	Point a(2.0,3.0);
	Point b(1.0,2.0);
	double dis = Distance(a,b);
	cout << dis << endl;
	return 0;
}

友元类
求原点到点的距离

#include<iostream>
using namespace std;
class Point
{
private:
	double x;
	double y;
public:
	Point(double a, double b);//构造函数
	friend class Tool;
};
//构造函数
Point::Point(double a, double b)
{
	this->x = a;
	this->y = b;
}
class Tool
{
public:
	double Distance(Point& b)
	{
		return sqrt(b.x * b.x + b.y * b.y);
	}
};
//点到原点距离
int main()
{	
	Point a(2.0,3.0);
	Tool t;
	double dis=t.Distance(a);
	cout << dis << endl;
	return 0;
}
C++学习笔记涵盖基础部分、核心编程和提高编程等内容。 ### 基础部分 包括初识C++、数据类型、运算符、程序流程结构、数组、函数、指针、结构体等内容[^1]。 ### C++核心编程 包含引用、函数提高、类和对象、文件操作等内容。在结构体方面,C++的结构体成员除了可以是变量,也可以是函数。例如: ```cpp #include <iostream> using namespace std; struct Student { char name[32]; int age; float score; void fun() { cout << "姓名:" << name << endl; cout << "年龄:" << age << endl; cout << "成绩:" << score << endl; } void fun1() { cout << "上课了" << endl; } }; int main() { Student stu; cin >> stu.name; cin >> stu.age; cin >> stu.score; stu.fun(); stu.fun1(); return 0; } ``` 此代码定义了一个`Student`结构体,其中包含变量和函数成员,并在`main`函数中使用该结构体创建对象并调用其成员函数[^2]。 ### C++关键字 包含asm、do、if、return、typedef等众多关键字,这些关键字在C++编程中有特定的用途和语法规则[^3]。 ### C++常量 用于记录程序中不可更改的数据,使用语法有两种: 1. `#define`宏常量:`#define 常量名 常量值` 2. `const`数据类型 常量名 = 常量值; 示例代码如下: ```cpp #include <iostream> using namespace std; // 1. #define 宏常量 #define Day 7 int main() { // int Day = 14; // 如果修改常量的值,会报错,宏常量不可修改 cout << "一周有" << Day << "天" << endl; // 2. const修饰的常量 const int month = 12; // int month = 17; // 常量定义后不可修改值,程序报错 cout << "一年有" << month << "个月" << endl; system("pause"); return 0; } ``` 上述代码通过两种方式定义了常量,并展示了常量不可修改的特性[^4]。 ### C++提高编程 包含模板、STL等内容[^1]。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值