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

博客:https://blog.slight-wind.com/

C++ 面向对象编程作业

第二章

2-5-3动态空间管理
#include<iostream>
#include<cstdio>
#include<cstring>
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<num;i++){
		cin>>a[i];
		if(a[i]>0)positive++;
		if(a[i]<0)negative++;
	}
	cout<<"There are "<<num<<" figures,\n"; 
	cout<<positive<<" of them are positive numbers,\n"; 
	cout<<negative<<" of them are negatives.\n";
	delete a;
	return 0;
}

2-5-2求圆的面积与周长
#include<iostream>
#include<cstdio>
#include<cstring>
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="<<s<<",c="<<c<<endl;
	return 0;
}

第三章

补充编程题3_5_6: 简单图书管理
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
class Book{
	public:
		Book(string name,float p,int n);
		void display(){
			cout<<bookname<<" "<<price<<" "<<number<<endl;
		}
		void borrow(){
			number--;
		}
		void restore(){
			number++;
		}
	private:
		string bookname;
		float price;
		int number;
};
Book::Book(string name,float p,int n){
	bookname=name;
	price=p;
	number=n;
}
int main(){
	char name[20]="C++";
	Book book1(name,23.5,3);
	strcpy(name,"Data Structure");
	Book book2(name,28.8,7);
	book1.borrow();
	book1.display();
	book2.restore();
	book2.display();
	return 0;
}
补充编程题3_5_5: 友元函数的定义与使用
#include<iostream>
#include<cstdio>
#include<cstring>
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:"<<S.name<<"   "<<S.score<<endl;
	cout<<"Teacher's name:"<<T.name<<"   "<<T.pro<<endl;
}
int main(){
	char stuname[20],teaname[20],teapro[20]; 
	cout<<"请输入学生姓名:"<<endl; 
	cin>>stuname; 
	cout<<"请输入教师姓名:"<<endl;
	cin>>teaname;
	cout<<"请输入教师职称:"<<endl;  
	cin>>teapro;
	Stu student(stuname,88);
	Tea teacher(teaname,teapro);
	print(student,teacher);
	return 0;
}
补充编程题3_5_1: 立方体类的定义与使用
#include<iostream>
#include<cstdio>
#include<cstring>
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<<"输入立方体的长宽高:"<<endl;
	cin>>l>>w>>h;
	Cube A(l,w,h);
	Cube B;
	cout<<A.Compute()<<endl;
	cout<<B.Compute()<<endl;
}
3-4-3 设计汽车类
#include<iostream>
#include<cstdio>
#include<cstring>
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<<car1.GetBrand()<<"|"<<car1.GetType()<<"|"<<car1.GetYear()<<"|"<<car1.GetPrice()<<endl; 
	Car car2; 
	cout<<car2.GetBrand()<<"|"<<car2.GetType()<<"|"<<car2.GetYear()<<"|"<<car2.GetPrice()<<endl; 
return 0; 
}
3-4-1设计学生类
#include<iostream>
#include<cstdio>
#include<cstring>
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<<stu[0].Getage( )<<","<<stu[0].Getname( )<<"\n";
	cout<<stu[1].Getage( )<<","<<stu[1].Getname( )<<"\n"; 
	cout<<stu[2].Getage( )<<","<<stu[2].Getname( )<<"\n"; /*这三句可改用一个循环*/
    return 0;
}
  • 7
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风好衣轻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值