C++类-成员,成员函数,构造函数,析构函数

C++类

成员,成员函数说明:


#include<iostream>

using namespace std;

class box
{
    
    
public:
	// 变量 成员
	double length = 2;
	double breadth;
	double height;
	// 方法 成员函数 
	double get();
	void set(double len, double bre, double hei);
};

double box::get()
{
    
    
	return length * breadth * height;
}

void box::set(double len, double bre, double hei)
{
    
    
	length = len;
	breadth = bre;
	height = hei;
}

int main()
{
    
    
	box box1, box2, box3;
	double volume = 0.0;

	// box1的描述
	box1.length = 5.0; box1.breadth = 6.0; box1.height = 7.0;

	// box2的描述
	box2.length = 10; box2.breadth = 12; box2.height = 13;

	volume = box1.length * box1.breadth * box1.height;
	cout << "box1的体积为:" << volume << endl;

	volume = box2.length * box2.breadth * box2.height;
	cout << "box2的体积为:" << volume << endl;

	// box3的相关描述
	box3.length = 1; box3.breadth = 2; box3.height = 3;
	volume = box3.get();

	cout << "box3的体积为:" << volume << endl;
	
	getchar();
	return 0;
}

修饰符和派生:

#include <iostream>

using namespace std;

class Box
{
    
    
private:
	double length; // 默认的是private类型,只能在类内部进行使用
protected:
	double width; // protected成员在类外部不可以被调用,但是在子类/派生类中可以被调用
public:
	double height; // 可以通过 .  直接进行访问
	
	// 成员函数,定义长度和宽度
	void setwidth(double wid);
	void setlength(double len); // 定义获取长度的成员函数

	// 成员函数  获取长度和宽度
	double getwidth()
	{
    
    
		return width;
	}
	double getlenth()
	{
    
    
		return length;
	}
};

void Box::setwidth(double wid)
{
    
    
	width = wid;
}

void Box::setlength(double len)
{
    
    
	length = len;
}

class SmallBox :Box
{
    
    
public:
	void setSmallBox(double wid, double hei);

	double SmallBox::getSmallWidth()
	{
    
    
		return width;
	}
	double SmallBox::getSmallHeight()
	{
    
    
		return height;
	}
};

void SmallBox::setSmallBox(double wid, double hei)
{
    
    
	// length是private类型,不能访问
	width = wid; // protected 类型修饰符 可以访问
	height = hei; // 公共  可以访问
}


int main()
{
    
    
	Box box1;
	SmallBox sbox1;

	// 获取长度和宽度
	box1.setlength(3); 
	box1.setwidth(2);
	// 获取高度
	box1.height = 1;
	
	// 访问子类
	// 设置子类的宽度和高度,但是长度是private类型,不能访问
	sbox1.setSmallBox(5,6);
	sbox1.getSmallWidth();

	cout << "box1 的长宽高为:" << box1.getlenth() << " " << box1.getwidth() << " "<< box1.height << endl;
	cout << "sbox的宽和高为:" << sbox1.getSmallWidth() << " " << sbox1.getSmallHeight() << endl;
	getchar();
	return 0;
}

构造函数,析构函数

#include <iostream>

using namespace std;

class Line
{
    
    
public:
	void setLineLength(double len);
	double getLineLength()
	{
    
    
		return length;
	}
	Line(double len); // 构造函数
	~Line(); // 析构函数

private:
	double length;
};

void Line::setLineLength(double len)
{
    
    
	length = len;
	cout << "设置直线的长度为:" << len << endl;
}

Line::Line(double len) :length(len)
{
    
    
	cout << "创建了对象,长度为:" << len << endl;
}

Line::~Line()
{
    
    
	// 在程序结束的时候会执行析构函数进行删除对象,即return 0之后,可以在命令行中查看
	cout << "删除了对象" << endl; 
}

int main()
{
    
    
	Line l(1); // 构造函数设置线段初始值
	cout << "构造函数设置的线段长度:" << l.getLineLength() << endl;

	l.setLineLength(2); // 成员函数设置初始值
	cout << "成员函数设置的线段长度:" << l.getLineLength() << endl;
	getchar();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wxc_1998/article/details/121176762