WKYNEKO

博客园 首页 新随笔 联系 订阅 管理

/*
1518: C++2第9章—circle拷贝构造函数
时间限制:1.000s 内存限制:128MB

题目描述
定义一个circle类,属性为半径,周长和面积。要求定义构造函数和拷贝构造函数及其他成员函数,实现以下功能:
根据输入的半径构造一个对象
能根据已知的对象生成一个的新对象,新对象的半径是个原对象半径的两倍
能获取对象属性值
输入格式
一个圆的半径
输出格式
先复制这个对象
输出复制后对象的半径,周长,面积
样例输入
1
样例输出
2
12.56
12.56


#include<iostream>
#include<iomanip>
#define PI 3.141
using namespace std;
class Circle
{


private:
int r;
double perimeter, area;
public:
Circle(int rr = 1);
Circle(const Circle&c);
void getperimeter()
{


cout << fixed << setprecision(2) << (2 * PI*r) << endl;
}
void getarea()
{


cout << fixed << setprecision(2) << (r*PI*r) << endl;
}
void getr()
{


cout << r << endl;
}
};
Circle::Circle(int rr) :r(rr){

}
Circle::Circle(const Circle&c)
{


r = 2 * c.r;
}
int main()
{


int R;
cin >> R;
Circle c1(R);
Circle c2 = c1;
c2.getr();
c2.getperimeter();
c2.getarea();

}*/

 

 

 

/*
1519: C++2第9章—Point_友元函数
时间限制:1.000s 内存限制:128MB

题目描述
定义一个Point类, 用来描述平面上的一个点.要求支持以Point A, B(0, 0)等方式完成对象的生成.定义友元函数Dist, 计算并返回两点之间的距离
输入格式
两个点的坐标
输出格式
两点之间的距离
样例输入
0 0 0 2
样例输出
2


#include<iostream>
#include<cmath>
using namespace std;
class Point{
private:
int x, y;
public:
Point()
{
cin >> x >> y;
}
friend double Dist(Point &p1, Point &p2);
};
double Dist(Point &p1, Point &p2)
{
return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
}
int main()
{
Point p1, p2;
cout << Dist(p1, p2) << endl;
}
*/

 

 


/*
1295: C++2第九章上机题3
时间限制:1.000s 内存限制:128MB

题目描述
修改教材习题第6题的程序,增加一个fun函数,改写main函数。在main函数中调用fun函数,在fun函数中调用change和display函数,实现student对象成员的显示和修改。在fun函数中使用对象的引用(Student&)作形参。

输入格式

输出格式
输出学生学号和成绩,两次修改后的成绩
样例输入

样例输出
101 78.5
101 80.5
101 81.5


#include <iostream>
using namespace std;
class Student
{
public:
Student(int n, float s) :num(n), score(s){}
void change(int n, float s) { num = n; score = s; }
void display() { cout << num << " " << score << endl; }
private:
int num;
float score;
};
int main()
{
Student stud(101, 78.5);
void fun(Student&);
fun(stud);
return 0;
}
void fun(Student &stu)
{
stu.display();
stu.change(101, 80.5);
stu.display();
stu.change(101, 81.5);
stu.display();
}
*/

 

posted on 2023-05-14 23:18  BKYNEKO  阅读(16)  评论(0编辑  收藏  举报