c++//构造函数

#include"iostream"
using namespace std;
class Swap
{
private:
	double n1, n2;
public:
	Swap(double x, double y)
	{
		n1 = x;
		n2 = y;
	}
	void swap()
	{
		double z;
		z = n1; n1 = n2; n2 = z;
	}
	void print()
	{
		cout << "交换后" << endl;
		cout << n1 <<"   "<< n2 << endl;
	}
};
void main()
{
	double x1, x2;
	cout << "请输入两个数:";	cin >> x1 >> x2;
	Swap t(x1, x2);
	t.swap();
	t.print();
}


#include <iostream>
using namespace std;
class Box
{
public:
	Box(int, int, int);
	int volume();
private:
	int length;
	int width;
	int height;
};
Box::Box(int l, int w, int h)
{
	length = l;
	width = w;
	height = h;
}
int Box::volume()
{
	return(length * width * height);
}
int main()
{
	Box box1(22, 25, 10);
	cout << "The volume of box1 is " << box1.volume() << endl;
	Box box2(21, 30, 15);
	cout << "The volume of box2 is " << box2.volume() << endl;
	return 0;
}

#include <iostream>
using namespace std;
class Box
{
public:
	Box()
	{
		length = 10;  width = 10; height = 10;
	}
	Box(int len, int w, int h)
	{
		length = len; width = w; height = h;
	}
	int volume();
private:
	int height;
	int width;
	int length;
};
int Box::volume()
{
	return(height * width * length);
}
int main()
{
	Box box1;
	cout << "The volume of box1 is " << box1.volume() << endl;
	Box box2(15, 30, 25);
	cout << "The volume of box2 is " << box2.volume() << endl;
	return 0;
}


#include <iostream>
using namespace std;
class Box
{
public:
	Box()
	{
		length = 10;  width = 10; height = 10;
	}
	Box(int len, int w, int h)
	{
		length = len; width = w; height = h;
	}
	int volume();
private:
	int height;
	int width;
	int length;
};
int Box::volume()
{
	return(height * width * length);
}
int main()
{
	Box box1;
	cout << "The volume of box1 is " << box1.volume() << endl;
	Box box2(15, 30, 25);
	cout << "The volume of box2 is " << box2.volume() << endl;
	return 0;
}


#include <iostream>
using namespace std;
class Box
{
public:
	Box(int len = 10, int w = 10, int h = 10)
	{
		length = len;  height = h;  width = w;
	}
	int volume()
	{
		return(height * width * length);
	}
private:
	int length;
	int width;
	int height;
};
int main()
{
	Box box1;
	cout << "The volume of box1 is"
		<< box1.volume() << endl;
	Box box2(15);
	cout << "The volume of box2 is"
		<< box2.volume() << endl;
	Box box3(15, 30);
	cout << "The volume of box3 is"
		<< box3.volume() << endl;
	Box box4(15, 30, 20);
	cout << "The volume of box4 is"
		<< box4.volume() << endl;
	return 0;
}

个人实践

#include"iostream"
using namespace std;
class Compare
{
public:
	Compare(int, int);
	void Judge();
private:
	int x, y;
};
Compare::Compare(int a, int b)
{
	x = a; y = b;
}
void Compare::Judge()
{
	if (x > y)cout << "两个数的关系是:" << x << "大于" << y;
	else if (x == y)cout << "两个数的关系是:" << x << "等于" << y;
	else cout << "两个数的关系是:" << x << "小于" << y;
}
int main()
{
	int QQ, VX;
	cout << "请输入两个数:";
	cin >> QQ >> VX;
	Compare haha(QQ, VX);
	haha.Judge();

}

#include"iostream"//****002
using namespace std;
class Compare
{
public:
	Compare(int a, int b) :x(a), y(b) {};//******强烈推荐 提高效率写法******
	void Judge();
private:
	int x, y;
};
void Compare::Judge()
{
	if (x > y)cout << "两个数的关系是:" << x << "大于" << y;
	else if (x == y)cout << "两个数的关系是:" << x << "等于" << y;
	else cout << "两个数的关系是:" << x << "小于" << y;
}
int main()
{
	int QQ, VX;
	cout << "请输入两个数:";
	cin >> QQ >> VX;
	Compare haha(QQ, VX);
	haha.Judge();
}

作业001

#include<iostream>
using namespace std;
#define PI 3.1415926
class round_area
{
public:
	round_area(int r);
	double count();
private:
	int R;
};
round_area::round_area(int r)
{
	R = r;
}
double round_area::count()
{
	return R * R * PI;
}
int main()
{
	int haha;
	cout << "请输入半径的值:";
	cin >> haha;
	round_area RR(haha);
	cout << "半径是" << haha << "的圆面积=" << RR.count();
}


002

#include"iostream"
using namespace std;
class Compare
{
public:
	Compare(int, int);
	void Judge();
private:
	int x, y;
};
Compare::Compare(int a, int b)
{
	x = a; y = b;
}
void Compare::Judge()
{
	if (x > y)cout << "两个数的关系是:" << x << "大于" << y;
	else if (x == y)cout << "两个数的关系是:" << x << "等于" << y;
	else cout << "两个数的关系是:" << x << "小于" << y;
}
int main()
{
	int QQ, VX;
	cout << "请输入两个数:";
	cin >> QQ >> VX;
	Compare haha(QQ, VX);
	haha.Judge();
}

003

#include"iostream"
using namespace std;
class Rectangle
{
public:Rectangle(int width, int length) :w(width), l(length) {};
	  int C(); int S();
private:int w, l;
};
int Rectangle::C()
{
	return (w + l);
}
int Rectangle::S()
{
	return (w * l);
}
int main()
{
	int a, b;
	int i = 0;
	while (i<3)
	{
		cout << "请输入矩形的长和宽:";
		cin >> a >> b;
		if (a < 0 || b < 0)cout << "输入错误,重新输入!\n";
		else
		{
			Rectangle haha(a, b);
			cout << "长方形的周长=" << haha.C() << endl;
			cout << "长方形的面积=" << haha.S() << endl;
		}
		i++;
	}
}

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

发布了38 篇原创文章 · 获赞 2 · 访问量 1196

猜你喜欢

转载自blog.csdn.net/weixin_44811068/article/details/103052185