c++面向对象基础练习题

五、编程题

定义一个借书证类 BookCard,在该类定义中包括如下内容:
私有成员变量:
char *stuName; //借书证学生的姓名
int id ; //借书证学生的学号
int number; //所借书的数量
再定义公有成员函数:
构造函数 //用来初始化 3 个数据成员
析构函数 //释放动态空间
display( ) //显示图书证的 3 个数据成员的信息
borrow( ) //将所借书的数量加 1,每个人限借 10 本
restore( ) //将所借书的数量减 1
在 main( )函数中,要求定义一个图书卡类的对象,在借阅和归还后分
别显示图书证的信息。请完成完整的程序

#include<iostream>
using namespace std;
class Book
{
 private :
    char *stuName;
    int id;
    int number;
public:
    Book(char n[10], int a,int b);
    ~Book();
    void display();
    void borrow();
    void restore();
};
   Book::Book(char n[10],int a,int b){
   	stuName = new char[10];
   	for(int i=0;i<10;i++){
   		stuName[i]=n[i];
	   }
   id = a;
   number = b;
   }
   
   Book::~Book(){
   }
   
   void Book::display(){
   	cout<<stuName<<endl;
   	cout<<id<<endl;
   	cout<<number<<endl;
   }
   
   void Book::borrow(){
   if(number<10){
   	number++;
   }
   }
   
   void Book::restore(){
   	number--;
   }
   
   int main(){	
   	Book book1("liming",2,3);
   	book1.borrow();
   	book1.display();
   	book1.restore();
   	book1.display();
   	return 0;
   }


定义学生类 Student,该类包括私有数据成员 char *name ,int age,分别表示学生的姓名和年龄。在该类中定义公有构造函数(动态分配内存 ,初始化学生的姓名和年龄)、析构函数(释放内存)和输出函数 print(显示学生的姓名和年龄)。再定义一个 Student 类的公有派生类 Postgrad(研究生类),其中增加私有数据成员 int credit,表示研究生的学分。在这个类中定义公有构造函数(初始化研究生的姓名、年龄、学分)和输出函数 print(显示研究生的姓名、年龄、学分)。
主函数的定义如下:
void main()
{ Postgrad pg("ZhangSan", 24, 9001); //姓名、年龄、学分
pg.print();
}
编写完整程序实现上述内容(包括类的定义和类的实现)。要求程序运行结果显示为:
name:ZhangSan
age:24
credit:9001

#include<iostream>
#include<string.h>
using namespace std;
class Student{
 private:
	char *name;
	int age;
 public:
 	Student(char *n,int a);
 	~Student();
 	void print(){
 		cout<<"name:"<<name<<endl;
	cout<<"age:"<<age<<endl;
	 }
};
Student::Student(char *n,int a){
	name = new char[strlen(n)+1];
	strcpy(name,n);
	age = a;
}
Student::~Student(){
	delete []name;
}

class Postgrad:public Student{
private:
	int credit;
public:
	Postgrad(char *n,int a,int c):Student(n,a)
	{
		credit = c;
	}
	void print(){
		Student::print();
		cout<<"credit:"<<credit<<endl;
	}
};
int main(){
	Postgrad pg("ZhangSan",24,9001);
	pg.print();
	return 0;
}

 定义复数类 Complex,有两个 double 类型的数据成员 real 和
imag,分别表示实部和虚部,请根据下列 main( )函数的代码完成类
的定义及相应运算符的重载,写出完整的程序代码,具体要求:
(5) 定义构造函数;
(6) 后置++运算符以成员函数形式重载;
(7) 输出流运算符以友元函数形式重载;
(8) 输入流运算符以友元函数形式重载
主函数如下:
void main( )
{
Complex b,c(3.2,7.8);
cin>>b;
cout<<b<<c;
b=c++ ;
cout<<b<<c ;
}
运行程序,输入:1 2<回车>后,结果显示如下:
1+2i
3.2+7.8i
3.2+7.8i
4.2+8.8i

#include<iostream>
using namespace std;
class Complex{
public:
	double real;
	double imag;
		Complex(double r=0,double i=0){
			real = r;
			imag = i;
		}
		Complex operator ++(int);
		friend ostream & operator <<(ostream &out, Complex &obj);
		friend istream & operator >>(istream &in, Complex &obj);
};
Complex Complex::operator++(int){//第一个Complex是返回值类型,第二个是类名 
   Complex temp(*this);
	real++;
	imag++;	 
	return temp;
}
ostream  & operator <<(ostream &out, Complex &obj){
	out<<obj.real;
	if(obj.imag!=0){
		if(obj.imag>0) out<<"+";
		out<<obj.imag<<"i";
	}
	out<<endl;
	return out;
}
istream  & operator >>(istream &in, Complex &obj){
	in>>obj.real>>obj.imag;
	return in;
}
int main(){
	Complex b;
	Complex c(3.2,7.8);
	cin>>b;
	cout<<b<<c;
	b=c++;
	cout<<b<<c;
	return 0;
}

 

 

定义点类 Point,有两个 double 类型的数据成员 x 和 y,分别表示横
坐标和纵坐标,请根据下列 main( )函数的代码完成类的定义及相应运算
符的重载,写出完整的程序代码,具体要求:
(1) 定义构造函数;
(2) 前置++运算符以成员函数形式重载;
(3) 输出流运算符以友元函数形式重载;
(4) 输入流运算符以友元函数形式重载。
void main( )
{ Point b,c(10,20);
cin>>b;
cout<<b<<c;
b=++c;
cout<<b<<c;
}
运行程序,当从键盘输入 5 10<回车>后,结果显示如下:
(5,10)
(10,20)
(11,21)
(11,21)

#include<iostream>
using namespace std;
class Point{
	public:
		double x,y;
		Point(double a=0,double b=0){
			x = a;
			y = b;
		}
		~Point(){
		}
		Point operator ++();
		//++i,加号后面有i就不用加东西  operator++()
		//i++,++后面还要接东西就operator++(int) 
		friend ostream &operator <<(ostream &out,Point &obj);
		friend istream &operator >>(istream &in,Point &obj);
};
Point Point::operator ++(){
	++x;
	++y;
	return *this;	
} 
ostream &operator <<(ostream &out, Point &obj){
	out<<"("<<obj.x<<","<<obj.y<<")"<<endl;
	return out;
}
istream &operator >>(istream &in, Point &obj){
	in>>obj.x>>obj.y;
	return in;
}
int main(){
	Point b;
	Point c(10,20);
	cin>>b;
	cout<<b<<c;
	b=++c;
	cout<<b<<c;
	return 0; 
}
	
	
	
	

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值