c++面向对象基础编程练习

1.是用一个类来记录学生成绩和他班级,里面有几个函数,静态成员和动态成员都有,包含学号,姓名,班级,成绩。计算总分,平均分。

下面展示一些 代码

#include<iostream>
#include<string>
using namespace std; 
class Student{
 private:
	string num;
	string name;
	string class_;
	float score;
	static double sum;
	static int cnt;
	static double ave;
public:
	void Show(){
		cout<<"num:"<<num<<endl;
		cout<<"name:"<<name<<endl;
		cout<<"class:"<<class_<<endl;
		cout<<"score:"<<score<<endl;
		cout<<endl;
	}
	Student(string Num,string Name,string Class_,float Score) 
	{
		num =Num;
		name = Name;
		class_ = Class_;
		score =Score;
		cnt++;
		sum+=score;
		
	}
	static double getTotal() //返回整型要用Static
		return sum;
	}
	static double average()
	{
		ave = sum/cnt;
		return ave;
	}
	static int Count()
	{
		return cnt;
	}
};
double Student::ave=0;
double Student::sum=0;
int  Student::cnt=0;
int main()
{
	Student Stu[5] = {
		Student("101","小明","十班",89.5),
		Student("102","Mike","十班",77.5),
		Student("103","Jackson","十班",87.0),
		Student("104","Cherry","十班",93.5),
		Student("105","Oscar","十班",89.0),
	};
	for(int i=0;i<5;i++)
	{
		Stu[i].Show();
	}
	cout<<Student::getTotal()<<endl;
	cout<<Student::average()<<endl;
	cout<<Student::Count()<<endl;
}
	

2.新建一个关于学生的类或结构体,包含学号,姓名,语文,数学,英语成绩;并输入一个整数n,分配一个类的数组。

下面展示一些 代码

#include<iostream>
#include<string>
using namespace std;
class Student{
	private:
		int num;
		string name;
		double  Chinese;
		double Math;
		double English;
	public:
		
		void input()
		{
			cout<<"输入学生学号 姓名 语文成绩 数学成绩 英语成绩"<<endl;
			cin>>num>>name>>Chinese>>Math>>English; 
		}
		void put()
		{
			cout<<"输出学生学号 姓名 语文成绩 数学成绩 英语成绩"<<endl;
			cout<<num<<name<<Chinese<<Math<<English<<endl; 
		}
};

int main()
{
	int n;
	cin>>n;
	Student *stu = new Student[n];
//两种方法 
//	Student *p=stu;
//	for(int i=0;i<n;i++)
//	   p->input();
//	p =stu;
//	for(int i=0;i<n;i++)
//	p->put();
for(int i=0;i<n;i++)
{
	stu[i].input();
}
for(int i=0;i<n;i++)
{
	stu[i].put();
}
 return 0;
}

3.运用上题的类,要求写三个函数。(均用二进制输入输出)(30分)

(1)把一个学生信息存到一个文件中
(2)从一个文件中提取所有学生信息,并打印
(3)从一个文件中复制所有学生信息,复制到另外一个文件。
下面展示代码:

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class Student{
	private:
		int num;
		char name[20]; //这里用char考虑到读是一个字符,一个字符的读
		double  Chinese;
		double Math;
		double English;
	public:
		
		void input()
		{
			cout<<"输入学生学号 姓名 语文成绩 数学成绩 英语成绩"<<endl;
			cin>>num>>name>>Chinese>>Math>>English; 
		}
		void put()
		{
			cout<<"输出学生学号 姓名 语文成绩 数学成绩 英语成绩"<<endl;
			cout<<num<<" "<<name<<" "<<Chinese<<" "<<Math<<" "<<English<<endl; 
		}
};
//保存到磁盘 
void saveToFile(Student s)
{
	ofstream outfile("stu.dat",ios::binary|ios::app);
	if(!outfile)
	{
		cerr<<"保存数据时打开sftu.dat文件错误!"<<endl; //cerr错误流输出 
		exit(1);  //exit(0):正常运行程序并退出程序; exit(1):非正常运行导致退出程序; return():返回函数 
	}
	outfile.write((char*)&s,sizeof(Student));
	outfile.close();
}
//从磁盘中取出
void putFile()
{
	ifstream infile("stu.dat",ios::binary);
	if(!infile)
	{
		cerr<<"从磁盘提取数据时打开文件stu.dat文件错误"<<endl;
		exit(1); 
	}
	Student s;
	while(infile.read((char*)&s,sizeof(Student)))
	{
		s.put();
	}
	infile.close();
 }
 //备份到磁盘
 void save()
 {
 	ifstream infile("stu.dat",ios::binary);
 	if(!infile)
	 {
	 	cerr<<"从磁盘提取数据时打开stu.dat文件错误"<<endl;
	 	exit(1);
	 	
	  } 
	ofstream outfile("stu.bak",ios::app|ios::binary);
	if(!outfile)
	{
	    cerr<<"保存数据时打开stu.bak文件错误"<<endl;
	 	exit(1);
		
	}
	infile.close();
	outfile.close();
  } 
  //读取备份文件
  void backup()
  {
  	ifstream infile("stu.dat",ios::binary);
  	if(!infile){
  		cout<<"error"<<endl;
  		exit(1);
	  }
	Student s;
	while(infile.read((char*)&s,sizeof(Student)))
	{
		s.put();
	}
	infile.close();
	
  }
   
int main()
{
	int n;
	cin>>n;
	Student *stu = new Student[n];
//两种方法 
//	Student *p=stu;
//	for(int i=0;i<n;i++)
//	   p->input();
//	p =stu;
//	for(int i=0;i<n;i++)
//	p->psaveToFile(stu[i]); 
for(int i=0;i<n;i++)
{
	stu[i].input();
	
}

cout<<"-------------------------------------输出学生信息----------------"<<endl; 
for(int i=0;i<n;i++)
{
	stu[i].put();
}
cout<<"----------------------------学生信息存入磁盘------------------"<<endl;
for(int i=0;i<n;i++)
{
saveToFile(stu[i]);
}
cout<<"---------------------------读文件----------------------------"<<endl; 
putFile();
cout<<"--------------------------备份-------------------------------"<<endl;
save();
backup();
 return 0;
}

4. 现有一个酒店场景。定义一个客人类Guest。包含成员属性:编号Num、姓名Name、房费Fee、当前酒店入住人数Count。其中编号Num需要程序自动生成。现在要求实现以下Guest的成员函数:构造函数、Show()显示Guest的信息、GetCount()返回当前酒店入住的人数、GetTotalIncome()返回当前酒店的总收入。并定义3个Guest对象来对成员函数进行测试。

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
double TotalIncome = 0;
int Num=0;
class Guest{
private:
	int Num;
	string Name;
	double Fee;
	static int count;
public:
	Guest(string name,double fee)
	{
		Num++;
		Name =name;
		Fee =fee;
		count++;
	    TotalIncome+=fee;
	}
	void Show()
	{
		cout<<"num:"<<Name<<" "<<"name:"<<Name<<" "<<"fee:"<<Fee<<endl;
	}
	static int GetCount()
	{
		return count;
	}
	static double GetTotalIcome()
	{
		return TotalIncome;
	}
	
};
int Guest::count=0;
int main()
{
	Guest g[3] = { 
		Guest("小明",342.0),
		Guest("丽丽",240.0),
		Guest("琳娜",360.0)
	};
	for(int i=0;i<3;i++)
	{
		g[i].Show();
	}
	cout<<"住入酒店的总人数:"<<g[0].GetCount()<<endl;//这里也可以用Guest::GetCount()
	cout<<"当前酒店总收入:"<<g[0].GetTotalIcome()<<endl; //Guest::GetTotalIncome
}

5.现有一抽象类Shape,它拥有一系列虚函数:Input()输入类需要的信息、Show()显示类的信息、Perimeter()计算周长、Area()计算面积。先定义Circle、Square、Triangle来继承Shape并实现其虚函数。要求创建Circle、Square、Triangle的对象,用基类指针指向这些对象,并调用成员函数进行测试。

#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
class Shape{
	//没有私有成员变量;
 public:
	virtual void Input()=0;
	virtual void Show()=0;
	virtual double Perimeter()=0;
	virtual double Area()=0; 
};
class Circle:public Shape{
	private:
		double r;
	public:
		void Input()
		{
			cout<<"请输入半径是:"<<endl; 
			cin>>r;
		}
		void Show()
		{
			cout<<"圆的半径是:"<<r<<endl;
			cout<<"圆的周长是:"<<Perimeter()<<endl;
			cout<<"圆的面积是:"<<Area()<<endl; 
		}
		double Perimeter(){
			return 2*3.14*r;
		}
		double Area()
		{
			return r*r*3.14;
		}
};
class Square:public Shape{
	private:
		double lenth,wedth;
	public:
		void Input()
		{
			cout<<"请输入矩形的长和宽分别是:"<<endl; 
			cin>>lenth>>wedth;
		}
		void Show()
		{
			cout<<"矩形的长和宽分别是:"<<lenth<<wedth<<endl;
			cout<<"矩形的周长是:"<<Perimeter()<<endl;
			cout<<"矩形的面积是:"<<Area()<<endl; 
		}
		double Perimeter(){
			return 2*(lenth+wedth);
		}
		double Area()
		{
			return lenth*wedth;
		}
};
class Triangle:public Shape{
	private:
		double A,B,C;
	public:
		void Input()
		{
			cout<<"请输入三角形的三条边长是:"<<endl; 
			cin>>A>>B>>C;
		}
		void Show()
		{
			cout<<"三角形的三条边长是:"<<A<<B<<C<<endl;
			cout<<"三角形的周长是:"<<Perimeter()<<endl;
			cout<<"三角形的面积是:"<<Area()<<endl; 
		}
		double Perimeter(){
			return A+B+C;
		}
		double Area()
		{
			double p = 0.5*(A+B+C);
			return sqrt(p*(p-A)*(p-B)*(p-C));
		}
};

int main()
{
	Shape *p;
	p= new Circle();
	p->Input();
	p->Area();
	p->Perimeter();
	p->Show();
	
	p= new Triangle();
	p->Input();
	p->Area();
	p->Perimeter();
	p->Show();
	
	p= new Square();
	p->Input();
	p->Area();
	p->Perimeter();
	p->Show();
	
	
}

在这里插入图片描述

6.类Person是一个描述人员信息的数据结构体,包括姓名(不定长)、性别、年龄。利用该结构体创建数组emp[5],调用自身的Get()方法可以输入人员的信息,并通过Show()方法显示输入的信息。请编写程序完成上述功能。

#include<iostream>
#include<string>
using namespace std;
class Person{
  private:
	string name;
	string sex;
	int age;
  public:
  	void Get()
  	{
  		cin>>name>>sex>>age;
	  }
	void Show()
	{
		cout<<"姓名:"<<name<<"性别:"<<sex<<"年龄:"<<age<<endl;
	}
};
int main()
{
	int i;
	Person *emp= new Person[5];
	for(i=0;i<5;i++)
	emp[i].Get();
	for(i=0;i<5;i++)
	emp[i].Show() ;
	

}
  • 4
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值