C++ 面向对象编程作业(一)

C++ 面向对象编程作业

这次的 C++ 作业好不容易整完了,发出来方便一下大家,欢迎参考借鉴~
顺便给我的小博客打个广告…

第二章

2-5-3动态空间管理
#include
#include
#include
using namespace std;
int main(){
	int *a=new int[20];
	int num,negative=0,positive=0;
	cin>>num;
	if(num<1||num>20){
		cout<<"number error.\n";
		delete a;
		return 0;
	}
	for(int i=0;i>a[i];
		if(a[i]>0)positive++;
		if(a[i]<0)negative++;
	}
	cout<<"There are "<
2-5-2求圆的面积与周长
#include
#include
#include
using namespace std;
const float pi=3.14159;
int main(){
	float r,s,c;
	cin>>r;
	s=pi*r*r;
	c=2*pi*r;
	cout<<"s="<

第三章

补充编程题3_5_6: 简单图书管理
#include
#include
#include
using namespace std;
class Book{
	public:
		Book(string name,float p,int n);
		void display(){
			cout<
补充编程题3_5_5: 友元函数的定义与使用
#include
#include
#include
using namespace std;
class Stu{
	public:
		Stu(string name,int score);
		friend void print();
		string name;
		int score;
};
class Tea{
	public:
		Tea(string name,string pro);
		friend void print();
		string name;
		string pro;
};
Stu::Stu(string na,int sc){
	name=na;
	score=sc;
}
Tea::Tea(string na,string pr){
	name=na;
	pro=pr;
}
void print(Stu S,Tea T){
	cout<<"student's name:"<>stuname; 
	cout<<"请输入教师姓名:"<>teaname;
	cout<<"请输入教师职称:"<>teapro;
	Stu student(stuname,88);
	Tea teacher(teaname,teapro);
	print(student,teacher);
	return 0;
}
补充编程题3_5_1: 立方体类的定义与使用
#include
#include
#include
using namespace std;
class Cube{
	public:
		Cube(int L=3,int W=2,int H=1);
		int L,W,H;
		int Compute(){
			return L*W*H;
		}
};
Cube::Cube(int l,int w,int h){
	L=l,W=w,H=h;
}
int main(){
	int l,w,h;
	cout<<"输入立方体的长宽高:"<>l>>w>>h;
	Cube A(l,w,h);
	Cube B;
	cout<
3-4-3 设计汽车类
#include
#include
#include
using namespace std;
class Car{
	private:
		string brand;
		string type;
		int year;
		double price;
	public:
		Car(void){
			brand="undefinition";
			type="undefinition";
			year=2000;
			price=0;
		}
		Car(string brand,string type,int year,double price);
		string GetBrand(){
			return brand;
		}
		string GetType(){
			return type;
		}
		int GetYear(){
			return year;
		}
		double GetPrice(){
			return price;
		}
};
Car::Car(string b,string t,int y,double p){
	brand=b;
	type=t;
	year=y;
	price=p;
}
int main(){ 
	Car car1("FIAT","Palio",2007,6.5); 
	cout<
3-4-1设计学生类
#include
#include
#include
using namespace std;
class Student{
	private:
		int age;
		string name;
	public:
		Student(int a, string m){
			age=a,name=m;
		}
		Student(){
			age=0,name="unnamed";
		}
		void SetMember(int a, string m){
			age=a,name=m;
		}
		int Getage(){
			return age;
		}
		string Getname(){
			return name;
		}
};

int main(){
	Student stu[3]={Student(13,"wang")} ;   /*第一个元素用带参构造函数初始化;第二、三个元素由无参构造函数初始化,默认年龄为 0 ,姓名为 "unnamed"*/
	stu[2].SetMember(12,"zhang");           /*修改第三个元素的数据成员值*/
	cout<

你可能感兴趣的:(C++,作业)