ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c++类的基本用法(析构和构造函数)

2021-07-03 19:04:39  阅读:146  来源: 互联网

标签:return cout int c++ Person 析构 include 构造函数


创建长方体类实现求体积和面积的操作,然后分别用类成员函数和全局函数判断俩个长方体是否相等


#include <iostream>
#include <stdio.h>
using namespace std;
class Cube {
protected:
	int m_H, m_W, m_L;
public:
	Cube(int h=0,int w=0,int l=0) {
		m_H = h, m_W = w, m_L = l;
	}
    int    getH() {
		return   m_H;
	}
	int  getW() {
		return   m_W;
	}
	int   getL() {
		return   m_L;
	}
	//面积
	int  calculateS() {
		return 2 * m_W * m_L + 2 * m_W * m_H + 2 * m_L * m_H;
	}
	//体积
	int calculateV()
	{
		return m_W * m_L * m_H;
	}
	//成员函数
	bool cube_equal(Cube a)
	{
		if (a.m_H == m_H && a.m_L == m_L && a.m_W == m_W)
			return true;

		return false;
	}
};
//全局函数
bool cube_equal(Cube a, Cube b)
{
	if (a.getH() == b.getH() && a.getL() == b.getL() && a.getW() == b.getW())
		return true;
	return false;
}
int main() {
	Cube c1(10, 10, 10);
	cout << c1.calculateV();
	Cube c2(10, 10, 11);
	bool flag = c1.cube_equal(c2);
	if (flag == true)   cout << "equal";
	else cout << "not equal";

	return 0;
}

创建圆类和点类并且用函数实现点和圆的关系判断(注释掉的部分实现了分文件编写)

#include <iostream>
#include "point.h"
#include <stdio.h>
#include <cmath>
using namespace std;
//点和圆的关系判断 
//点和圆类
//class Point {
//	int m_x, m_y;
//public:
//	Point(int x = 0, int y = 0)
//	{
//		m_x = x, m_y = y;
//	}
//	int  getX() {
//		return m_x;
//	}
//	int  getY() {
//		return m_y;
//	}
//	void show() {
//		cout << m_x << m_y;
//	}
//};
class Circle {
	Point m_p;
	int m_r;
public:
	int  getY() {
		return m_p.getY();
	}
	int getR() {
		return m_r;
	}
	int  getX() {
		return m_p.getX();
	}
	Circle(Point p = { 0,0 }, int r = 0) {
		m_p = p, m_r = r;
	}
};
void is_in_circle(Circle a, Point b) {
	int distance = pow(a.getX() - b.getX(), 2) + pow(a.getY() - b.getY(), 2);
	if (distance == a.getR() * a.getR())  cout << "该点在圆上";
	else if (distance < a.getR() * a.getR())  cout << "该点在圆内";
	else cout << "该点在圆外";
}
int main() {
	//创建圆 
	Circle c({ 10,0 }, 10);
	//创建点 
	Point b(0, 0);
	is_in_circle(c, b);
	return 0;
}

构造和析构函数:
对象的清理和初始化

#include <iostream>
#include <stdio.h>
using namespace std;
//对象的初始化和清理是非常重要的事情,不做很容易产生问题
//构造函数和析构函数(编译器会提供并且自动实现,但函数体里面是空的)
//自己写的话编译器会自动调用你写的析构和构造函数 
//构造函数:给对象的一个初始化操作
//析构函数: 在对象销毁前自动调用,执行清理操作

//构造函数:1.无返回值不写void 2.函数名称与类名相同 3.构造函数可以有参数,可以发生重载
// 4.创建对象时自动调用且只调用1次 
//析构函数:与构造函数不同 其名字为~类名 ,**析构函数只能有1个**,销毁前会自动调用析构函数 
class Person {
   public:
	Person(){
		cout <<"Person构造函数的调用"<<endl;
	} 
	~Person(){
		cout <<"Person析构函数的调用"<<endl; 
	}
};
void test01(){
		Person p;
}
int main(){
	
//test01();
Person p;
	system("pause");
	return 0;
}

拷贝构造的调用时机:
1.使用一个创建完毕的对象来初始化一个新对象
2.值传递的方式给函数参数传值
3. 以值方式返回局部对象

默认情况下,c++编译器至少给一个类添加3个函数

1.默认构造函数(无参,函数体为空)

2.默认析构函数(无参,函数体为空)

3.默认拷贝构造函数,对属性进行值拷贝

构造函数调用规则如下:

如果用户定义有参构造函数,c++不在提供默认无参构造,但是会提供默认拷贝构造

如果用户定义拷贝构造函数,c++不会再提供其他构造函数

深拷贝和浅拷贝

在这里插入图片描述

#include <iostream>
#include <stdio.h>
using namespace std;
//浅拷贝:简单的赋值拷贝操作,浅拷贝带来的问题就是堆区的内存重复释放
//深拷贝:在堆区重新申请空间,进行拷贝操作 ,避免堆区的内存重复释放
class Person {

public:
	Person() {
		cout << "Persson的无参构造" << endl;
	}
	Person(int age, int height) {
		m_age = age;
		m_height = new int(height);
		cout << "Persson的有参构造" << endl;
	}
	Person(const Person& a) {
		m_age = a.m_age;
		//m_height = a.m_height;编译器默认实现这一行,浅拷贝
		//深拷贝
		m_height = new int(*a.m_height);
	}
	~Person() {//浅拷贝带来的问题就是堆区的内存重复释放
		if (m_height != NULL)
		{
			delete m_height;
			m_height = NULL;
		}
		cout << "Person的析构函数" << endl;
	}
	int m_age;
	int* m_height;
};
void test01() {
	Person p1(18, 200);
	Person p2(p1);//浅拷贝 
	cout << p2.m_age << *p2.m_height;
}
int main() {
	test01();

	return 0;
}

初始化列表(初始化操作更简单)

#include <iostream>
#include <stdio.h>
using namespace std;
//初始化列表:作属性的初始化 
//构造函数(形参列表):属性1(形参),属性2(形参)...{} 
class Person{

	public:
		Person(int a,int b ,int c):m_a(a),m_b(b),m_c(c){
		}
		int m_a,m_b,m_c;
};
int main(){
	Person c(20,30,40);
	cout <<c.m_a<<" "<<c.m_b<<" "<<c.m_c<<endl;
	
	return 0;
}

类对象作为类成员时析构和构造的调用顺序

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
//类对象作为类成员
// c++类中的成员是另外一个类 
class Phone {
public:
	Phone(string pName) {
		m_pName = pName;
		cout << "Phone的构造函数" << endl;
	}
	~Phone() {
		cout << "Phone的析构函数" << endl;
	}
	string m_pName;
};
class Person {
public:
	Person(string Name, string pName) :m_Name(Name), m_Phone(pName) {
		cout << "Person的构造函数" << endl;
	}
	~Person() {
		cout << "Person的析构函数" << endl;
	}
	string m_Name;
	Phone m_Phone;
};
void test01(){
	//先调用对象成员的构造,然后调用本类的构造
	//析构相反 
	Person p("张三", "苹果");
	cout << p.m_Name<<p.m_Phone.m_pName<<endl;
}
int main() {
	test01();

	return 0;
}

静态成员变量

#include<iostream>
#include <stdio.h>
using namespace std;
//静态成员 static ,也是有访问权限的和其他成员一样 
//1.所有对象共享同一份数据 2.在编译阶段分配内存 3.**类内声明,类外初始化**
//静态成员函数 :1.所有对象共享一个函数
class Person{
	public: 
		static int m_a;
		
}; 
 int Person::m_a =  100;//**
void test01(){
	Person p;
	cout <<p.m_a<<endl;
	Person p2;
	p2.m_a=200;
	cout <<p.m_a<<endl;
}
//静态成员可以直接通过对象访问或者类名访问 
void test02(){
	cout <<Person::m_a<<endl;
	Person p;
	cout <<p.m_a;
}
int main(){
//	test01();
	test02();
	return 0;
}

静态成员函数
也有对应的权限,注意事项在代码里面

#include <iostream>
#include <stdio.h>
using namespace std;
//静态成员函数
//所有对象共享一个函数
//静态成员函数只能访问静态成员变量 
class Person{
	
	public:
		int m_b;
	static 	void func(){
		m_a=100;
	//	m_b=200;//静态成员函数不可以访问非静态成员变量(无法区分因为所有对象都可访问) 
		cout <<"static 	void funcd的调用"<<endl;	
		}
		static int m_a;
}; 
int Person::m_a=0;
void test01(){
	Person::func();
}
int main(){
	test01();
	
	return 0;
}

标签:return,cout,int,c++,Person,析构,include,构造函数
来源: https://blog.csdn.net/m0_46362536/article/details/118422825

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有