C++笔记

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

#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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值