C++ 面向对象编程作业
这次的 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;
}