C++实验二

1. 声明一个树Tree类,有数据成员age(树龄)、构造函数、成员函数grow表示树龄的增加,成员函数showAge()显示对象树龄等。请初始化一棵18树龄的树,再分别调用其成员函数 进行测试。

#include <iostream>
using namespace std;

class Tree
{
	public:
	  Tree(int a):age(a){}
	  void grow()   
	  {
	  	age++; cout<<"grew"<<endl;
	  }
	  void showAge()
	  {
	  	cout<<"age=" <<age <<endl;
	  }
	private:
	  int age;	
};

int main()
{
	Tree t(18);
	t.grow();
	t.showAge();
	t.grow();
	t.showAge();
	return 0;
}

2. 设计一个学生类,输入学生的学号,以及三门课程成绩,输出平均成绩,并输出是否通过(假如任意一门成绩小于60则没通过)。

#include <iostream>
#include <string>
using namespace std;

class Student
{
	public:
	  Student(string iD, int c1, int c2, int c3):
	  		  id(iD), course1(c1), course2(c2), course3(c3){}
	  void getAver();
	  void judgeGrade();
	  void getID()
	  {
	  	cout<<"ID : "<<id<<endl;
	  }
	private:
	  string id;
	  int course1;
	  int course2;
	  int course3;	
};

int main()
{
	string idd;
	int c1, c2, c3;
	cin>>idd>>c1>>c2>>c3;
	Student s(idd, c1, c2, c3);
	s.getID();
	s.getAver();
	s.judgeGrade();
	return 0;
}
void Student::getAver()
{
	cout<<"Average grade : "<<(double)(course1+course2+course3)/3<<endl;
}
void Student::judgeGrade()
{
	if(course1<60 || course2<60 || course3<60)
	  cout<<"NOT PASSED"<<endl; 
	else 
	  cout<<"PASSED"<<endl;
	
}

3. 组合类实例。定义一个点Point类,有点坐标属性(x, y)。再定义一个矩形Rectangle类,其属性为两个Point点(分别表示矩形对角线上两点的坐标),及含有计算矩形的周长及面积的成员函数;再编写两个Rectangle类的友元函数,分别计算两个点组成的矩形的周长与面积。在主函数中进行测试。

#include <iostream>
#include <cmath>
using namespace std;

class Point
{
	public:
	  Point(){};
	  Point(double xx, double yy):x(xx), y(yy){}
	  Point(Point &p)
	  {
	  	x=p.x;
	  	y=p.y;
	  }
	  double getX() const { return x; }
	  double getY() const { return y; }
	private:
	  double x;	
	  double y;
};
class Rectangle
{
	public:
	  Rectangle(Point p1, Point p2):point1(p1), point2(p2){} 
	  double Perimeter();
	  double Area();
	  
	friend double perimeter(const Rectangle &rec);
	friend double area(const Rectangle &rec);
	
	private:
	  Point point1;	
	  Point point2;
};
double perimeter(const Rectangle &rec)
{
	return (abs(rec.point1.getX()-rec.point2.getX())+abs(rec.point1.getY()-rec.point2.getY()))*2;
}
double area(const Rectangle &rec)
{
	return abs(rec.point1.getX()-rec.point2.getX())*abs(rec.point1.getY()-rec.point2.getY());
}
int main()
{
	int x1, y1, x2, y2;
	cin>>x1>>y1>>x2>>y2;
	Point p1(x1, y1);
	Point p2(x2, y2);	
	Rectangle rec(p1, p2);
	cout <<"Prerimeter = "<<rec.Perimeter() <<"\tArea = "<< rec.Area() <<endl;
	cout <<"Prerimeter = "<<perimeter(rec) <<"\tArea = "<< area(rec) <<endl;
	return 0;
}
double Rectangle::Perimeter()
{
	return (abs(point1.getX()-point2.getX())+abs(point1.getY()-point2.getY()))*2;
}
double Rectangle::Area()
{
	return abs(point1.getX()-point2.getX())*abs(point1.getY()-point2.getY());
}


4. 设计一个学生信息管理程序,管理8个学生信息,能够按学号查询输出指定学生的信息(姓名,课程,成绩),也能输出全部学生信息。

要求:1)输出函数采用非成员函数形式,以对象指针为形参;

           2)在主函数中采用对象数组形式实例化8个学生对象;

           3)测试两种输出形式。

#include <iostream>
#include <string>
using namespace std;

class Student
{
	public:
	  Student(string idd, string namee, string cour, int a);
	  string getID(){ return id; }
	  string getName(){ return name; }
	  string getCourse(){ return course; }
	  int getScore(){ return score; }
	private:
	  string id;
	  string name;
	  string course;
	  int score;
};
void get(Student *s, string ID);

int main()
{
	Student stu[8] = {Student("02", "zhao",  "C++" , 90), 
					  Student("03", "qian",  "C"   , 99),
					  Student("06", "sun",   "Math", 96),
					  Student("07", "li",    "Eng" , 88),
					  Student("13", "zhou",  "C++" , 87),
					  Student("16", "wu",    "C"   , 83),
					  Student("17", "zheng", "Math", 81),
					  Student("27", "wang",  "Eng" , 91)};
	bool flag=1;
	string number;
	while(flag)
	  {
	    cout<<"PLEASE INPUT A NUMBER OR ALL: ";
	  	cin>>number;
	  	get(stu, number);
	  	cout<<"\nIF CONTINUE, INPUT '1', ELSE INPUT 0\n";
	  	cin>>flag;
	  	
	  }	
	return 0;
}

Student::Student(string idd, string namee, string cour, int a)
{
	id = idd;
	name = namee;
	course = cour;
	score = a;
}
void get(Student *s, string ID)
{
	bool flag=0;
	if(ID=="all" || ID=="ALL")
	  {
	  	cout<<"\tALL INFORMATION IS BELOW:\n";
		for(int i=0;i<8;i++)
		  {
		  	cout<<"ID:"<<s[i].getID()<<"\tName:"<<s[i].getName()<<"\tCourse:";
		  	cout<<s[i].getCourse()<<"\tScore:"<<s[i].getScore()<<endl;
		  }
	  }
	else
	  {
	  	for(int i=0;i<8;i++)
		  {
		  	if(s[i].getID()==ID)
		  	  {
		  	  	flag=1;
		  	  	cout<<"ID:"<<s[i].getID()<<"\tName:"<<s[i].getName()<<"\tCourse:";
		  		cout<<s[i].getCourse()<<"\tScore:"<<s[i].getScore()<<endl;
		  		break;
		  	  }
		  }
		if(!flag) cout<<"ERROR"<<endl;
	  }	
	
}

5. 采用标准vector动态数组存储学生对象改写第4题程序,并在类中增加静态数据成员count,实现对象生成的计数。

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Student
{
	public:
	  Student(){}
	  Student(string idd, string namee, string cour, int a);
	  void set(string idd, string namee, string cour, int a);
	  string getID(){ return id; }
	  string getName(){ return name; }
	  string getCourse(){ return course; }
	  int getScore(){ return score; }
	  static int getCount() { return count; }
	private:
	  string id;
	  string name;
	  string course;
	  int score;
	  static int count;
};
int Student::count = 0;
void get(vector<Student> &s, string ID); 
int main()
{
	string idd;
	string namee;
	string cour;
	int scoree;
	Student s;
	string number;
	int flag;
	vector<Student> stu;
	cout<<"IF SEARCH, PRESS '1', IF INPUT, PRESS '2', IF END, PRESS '0'\n";
	cin>>flag;
	while(flag)
	  {	  	
	  	if(flag==1)
	  	  {
	  	  	cout<<"INPUT ID:";
	  	  	cin>>number;
	  	  	get(stu, number);
	  	  	cout<<"\nIF SEARCH, PRESS '1', IF INPUT, PRESS '2', IF END, PRESS '0'\n";
	  		cin>>flag;
	  	  }
	  	if(flag==2)
	  	  {
	  	  	cout<<"INPUT ID NAME COURSE AND SCORE"<<endl;
	  	  	cin>>idd>>namee>>cour>>scoree;
	  		s.set(idd, namee, cour, scoree);
	  		stu.push_back(s);
	  		cout<<"\nIF SEARCH, PRESS '1', IF INPUT, PRESS '2', IF END, PRESS '0'\n";
	  		cin>>flag;
	  	  }
	  }
	cout<<"\ncount="<<Student::getCount()<<endl;
	return 0;
}

Student::Student(string idd, string namee, string cour, int a)
{
	id = idd;
	name = namee;
	course = cour;
	score = a;
	count++;
}
void Student::set(string idd, string namee, string cour, int a)
{
	id = idd;
	name = namee;
	course = cour;
	score = a;
	count++;
}
void get(vector<Student> &s, string ID)
{
	bool flag=0;
	if(ID=="all" || ID=="ALL")
	  {
	  	cout<<"\tALL INFORMATION IS BELOW:\n";
		for(int i=0;i<s.size();i++)
		  {
		  	cout<<"ID:"<<s.at(i).getID()<<"\tName:"<<s.at(i).getName()<<"\tCourse:";
		  	cout<<s.at(i).getCourse()<<"\tScore:"<<s.at(i).getScore()<<endl;
		  }
	  }
	else
	  {
	  	for(int i=0;i<s.size();i++)
		  {
		  	if(s.at(i).getID()==ID)
		  	  {
		  	  	flag=1;
		  	  	cout<<"ID:"<<s.at(i).getID()<<"\tName:"<<s.at(i).getName()<<"\tCourse:";
		  		cout<<s.at(i).getCourse()<<"\tScore:"<<s.at(i).getScore()<<endl;
		  		break;
		  	  }
		  }
		if(!flag) cout<<"ERROR"<<endl;
	  }		
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值