海院23级C++考试复习2

一个矩形体育场如图所示,现在需要在其周围建一个矩形过道,并在四周安上栅栏。栅栏的价格为
50元/米,过道造价为240元/平方米,过道宽为3米,体育场的长宽由键盘输入,,计算过道和栅栏
的造价。

9.1
#include <iostream>
using namespace std;
class Rectangle
{
private:
	double length;//数据成员 
	double width;
public:
	Rectangle(double Length=10.,double Width=5.);
	double Area();
	double SideLength();
};

int main()
{
	int a=50,b=240;
	double x,y;
	cout<<"请输入矩形的长和宽:";
	cin>>x>>y;
	cout<<endl;
	Rectangle rect1(x,y),rect2(x+3,y+3);
	cout<<"栅栏的长度为:"<<rect2.SideLength() 
<<",造价为:"<<rect2.SideLength()*a<<endl;
    double area12;
	area12=rect2.Area ()-rect1.Area ();
	cout<<"过道的面积为:"<<area12 <<",造价为:"<<area12*b<<endl;
	    return 0;
}
Rectangle::Rectangle (double Length,double Width)
{
	length=Length;
	width=Width;
}
double Rectangle::Area ()
{
	return length*width;
}
double Rectangle::SideLength ()
{
	return 2*(length+width);
}

定义一个复数类complex,使得下面代码能供工作
complex c1(3,5)
complex c2=4.5
c1.add(c2)
c1.show

9.2
#include<iostream>
using namespace std;
class complex{
	public:
		complex(double r,double i):real(r),image(i){//构造函数中用初始化列表初始化数据成员 
		}
	
		complex(double r):real(r),image(0){
		}
	
		void show(){
			cout<<real<<(image>0)?"+":"";
			cout<<image<<"i";
			cout<<endl;
		}
		
		void add(complex c2);
		private:
			double real, image;
};
void complex::add(complex c2){
	real+=c2.real;//real=real+c2.real 
	image+=c2.image;//image=image+c2.image
}

int main()
{
complex c1(1.5,-2.5);
complex c2=4.5;
c1.show();
c1.add(c2);
c1.show();
return 0;
}
子对象练习:Circle类的属性:圆心的坐标是Point类的一个对象。
创建对象进行操作

9.3
#include<iostream>
#include<string>
using namespace std;
class Point  
{
	public:
		Point();//默认构造函数
	   
		Point(int,int);//有2个参数构造函数
		void displayxy();
    private:
		int X,Y; 
};
//点类的实现部分
Point::Point ()
{
	X=7;
	Y=8;
    cout<<"Default constructor is called! ";
    displayxy();
}

Point::Point (int x,int y)
{
	X=x;
	Y=y;
    
}
void Point::displayxy()
{
	cout<<"("<<X<<","<<Y<<")"<<endl;
}
class Circle
{
public: Circle(float,int,int);
	    double area();
	    void display();
private: float radius;
         Point p;

};
Circle::Circle(float r,int x,int y):p(x,y)//,radius(r)
{
	//p.X=x;
	//p.Y=y;
	r=y;
}
double Circle::area()
{
 return 3.1415926*radius*radius;
}
void Circle::display()

{
cout<<"半径是:"<<radius<<"的圆面积是:"<<area();
cout<<"圆心的位置是:";
p.displayxy();	
}
int main()
{
  Circle c(2.0,1,1);
  //cout<<c.area()<<endl;
c.display();
return 0;
}
静态数据成员和静态成员函数:
写一个学生类,1)输出每个学生的姓名,学号,成绩;
2)统计并输出学生的总人数,总成绩,平均成绩,最高成绩,最低成绩

9.4

#include <iostream>
using namespace std;
class Student
{
	int no;
	string name;
	double score;
	static int totalNumber;
	static double totalScore;
	static double lowestScore;
	static double highestScore;
public:
	Student(int no_,string name_,double score_);
	static void Output();
	void StudentInformation();
};
int Student::totalNumber=0;
double Student::totalScore =0;
double Student::highestScore =0;
double Student::lowestScore =100;
int main()
{
	Student stu1(1001,"张三",97.5);
	stu1.StudentInformation ();
	Student stu2(1002,"李四",83.);
	stu2.StudentInformation ();
	Student stu3(1003,"王五",93.);
	stu3.StudentInformation ();
	Student stu4(1004,"郭六",62.5);
	stu4.StudentInformation ();
	Student stu5(1005,"任七",77.); 
	stu5.StudentInformation ();
	Student::Output ();
	return 0;
}
Student::Student (int no_,string name_,double score_)
{
	no=no_;
	name=name_;
	score=score_;
	totalNumber++;
	totalScore+=score;
	if (score>highestScore)
		highestScore=score;
	if (score<lowestScore)
		lowestScore=score;

}
void Student::StudentInformation ()
{
	cout<<"学号:"<<no<<"    "<<"姓名:"<<name<<"    "
<<"成绩:"<<score<<endl;
}
void Student::Output ()
{
	cout<<"学生总数:"<<totalNumber<<"    "<<"总成绩:"
<<totalScore<<"    "<<endl;
	cout<<"平均成绩:"<<totalScore/totalNumber<<"   "
<<"最高成绩"<<highestScore<<"    "
<<"最低成绩"<<lowestScore<<"    "<<endl;
}

定义复数类和二维向量类,复数具有实部和虚部,二维向量有两个向量。
利用友元类将复数转换为二维向量

9.5

#include <iostream>
using namespace std;
class Complex 
{ 
private:
	double real; 
	double imag; 
public: 
	Complex(double r,double i); 
	friend class Vector; 
}; 
class Vector{ 
private:
	double x,y; 
public: 
void Change(Complex c); 
void Print(Complex c); 
};
int main() 
{ 
	Complex c(1.2,3.4); 
	Vector v; 
	v.Change (c); 
	v.Print (c); 
	Complex b(1.2,-3.4); 
	Vector m; 
	m.Change (b); 
	m.Print (b); 
	return 0; 
}
Complex::Complex(double r,double i) 
{ 
	real=r; 
	imag=i; 
} 
void Vector::Change(Complex c) 
{ 
	x=c.real; 
	y=c.imag; 
} 
void Vector::Print(Complex c) 
{ 
//输出复数
	cout<<"复数:"; 
	cout<<c.real; 
	if(c.imag>0) 
	cout<<"+"; 
	cout<<c.imag<<"i"<<endl; 
//输出二维向量
	cout<<"二维向量:"; 
	cout<<"("<<x<<","<<y<<")"<<endl; 
}

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值