程序设计基础(实验九)
学号: 姓名: 专业:
1.目的:熟练掌握在vc环境下类的建立。
2 题目:
(1)定义圆类,完成面积的求解;定义圆柱体类,其中一数据成员为圆类,要求编写函数完成圆柱体体积和表面积的求解。
(2)定义stack类,完成栈功能的实现,包括push函数、pop函数、构造函数(完成空间的分配)、析构函数(完成空间的释放),添加断点,。
(3)类的建立与访问,添加私有成员number,添加静态私有成员obj_num
要求1:在构造函数中初始化number的值,生成对象obj1,并利用函数返回私有成员number的值
要求2:利用obj_num统计对象的个数,生成两个对象,利用函数读取obj_num的值
要求3:利用new函数生成该类的对象,调用delete函数进行删除,观察该过程是否调用了构造函数和析构函数(截图说明)
3 程序结果贴图(注意标明对应题号和进行简短说明)
图一 题目(1)程序运行截图
图二 题目(2)程序运行截图
obj1.number= 3
Nowobj_num = 1
obj2.number= 4355776
Nowobj_num = 2
obj3.number= 0
Nowobj_num = 3
obj4.number= 6
Nowobj_num = 4
Thedestructor has been called.
Nowobj_num = 3
***********************************************************
obj5.number= 19
Nowobj_num = 4
Thedestructor has been called.
obj6.number= 20
Nowobj_num = 4
Thedestructor has been called.
obj7.number= 21
Nowobj_num = 4
Thedestructor has been called.
obj8.number= 22
Nowobj_num = 4
Thedestructor has been called.
obj9.number= 23
Nowobj_num = 4
The destructorhas been called.
obj10.number= 24
Nowobj_num = 4
Thedestructor has been called.
obj11.number= 25
Nowobj_num = 4
Thedestructor has been called.
obj12.number= 26
Nowobj_num = 4
Thedestructor has been called.
obj13.number= 27
Nowobj_num = 4
Thedestructor has been called.
obj14.number= 28
Nowobj_num = 4
Thedestructor has been called.
obj15.number= 29
Nowobj_num = 4
Thedestructor has been called.
obj16.number= 30
Nowobj_num = 4
Thedestructor has been called.
obj17.number= 31
Nowobj_num = 4
Thedestructor has been called.
obj18.number= 32
Nowobj_num = 4
Thedestructor has been called.
obj19.number= 33
Nowobj_num = 4
Thedestructor has been called.
obj20.number= 34
Nowobj_num = 4
Thedestructor has been called.
obj21.number= 35
Nowobj_num = 4
Thedestructor has been called.
obj22.number= 36
Nowobj_num = 4
Thedestructor has been called.
obj23.number= 37
Nowobj_num = 4
Thedestructor has been called.
obj24.number= 38
Nowobj_num = 4
Thedestructor has been called.
***********************************************************
obj25.number= 26
Nowobj_num = 4
obj26.number= 27
Nowobj_num = 5
obj27.number= 28
Nowobj_num = 6
obj28.number= 29
Nowobj_num = 7
obj29.number= 30
Nowobj_num = 8
obj30.number= 31
Nowobj_num = 9
obj31.number= 32
Nowobj_num = 10
obj32.number= 33
Nowobj_num = 11
obj33.number= 34
Nowobj_num = 12
obj34.number= 35
Nowobj_num = 13
obj35.number= 36
Nowobj_num = 14
obj36.number= 37
Nowobj_num = 15
obj37.number= 38
Nowobj_num = 16
obj38.number= 39
Nowobj_num = 17
obj39.number= 40
Nowobj_num = 18
obj40.number= 41
Nowobj_num = 19
obj41.number= 42
Nowobj_num = 20
obj42.number= 43
Nowobj_num = 21
obj43.number= 44
Nowobj_num = 22
obj44.number= 45
Nowobj_num = 23
***********************************************************
obj45.number= 29
Thedestructor has been called.
Nowobj_num = 23
obj46.number= 30
Thedestructor has been called.
Nowobj_num = 23
obj47.number= 31
Thedestructor has been called.
Nowobj_num = 23
obj48.number= 32
Thedestructor has been called.
Nowobj_num = 23
obj49.number= 33
Thedestructor has been called.
Nowobj_num = 23
obj50.number= 34
Thedestructor has been called.
Nowobj_num = 23
obj51.number= 35
Thedestructor has been called.
Nowobj_num = 23
obj52.number= 36
Thedestructor has been called.
Nowobj_num = 23
obj53.number= 37
Thedestructor has been called.
Nowobj_num = 23
obj54.number= 38
Thedestructor has been called.
Nowobj_num = 23
obj55.number= 39
Thedestructor has been called.
Nowobj_num = 23
obj56.number= 40
Thedestructor has been called.
Nowobj_num = 23
obj57.number= 41
Thedestructor has been called.
Nowobj_num = 23
obj58.number= 42
Thedestructor has been called.
Nowobj_num = 23
obj59.number= 43
Thedestructor has been called.
Nowobj_num = 23
obj60.number= 44
Thedestructor has been called.
Nowobj_num = 23
obj61.number= 45
Thedestructor has been called.
Nowobj_num = 23
obj62.number= 46
Thedestructor has been called.
Nowobj_num = 23
obj63.number= 47
Thedestructor has been called.
Nowobj_num = 23
obj64.number= 48
Thedestructor has been called.
Nowobj_num = 23
Thescreen will be closed in 60 seconds.
题目(3)程序输出数据
4 编程过程中碰到的问题及总结
5 附源代码:
//题目(1)源代码:
// _circle.h: interface for the _circle class.
//
//////////////////////////////////////////////////////////////////////
#if!defined(AFX__CIRCLE_H__F43D814D_EC95_4C34_9227_24B6BA7742F3__INCLUDED_)
#define AFX__CIRCLE_H__F43D814D_EC95_4C34_9227_24B6BA7742F3__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif //_MSC_VER > 1000
class _circle
{
public:
_circle(double);
~_circle();
double area();
void display();
private:
double r,s;
};
#endif //!defined(AFX__CIRCLE_H__F43D814D_EC95_4C34_9227_24B6BA7742F3__INCLUDED_)
// _circle.cpp: implementation of the _circle class.
//
//////////////////////////////////////////////////////////////////////
#include "_circle.h"
#include <iostream>
#define _pi 3.1415926
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
_circle::_circle(double R)
{
r=R;
s=_circle::area();
}
_circle::~_circle()
{
std::cout<<"The circle destructor has beencalled."<<std::endl;
}
double _circle::area()
{
return _pi*r*r;
}
void _circle::display()
{
std::cout<<"The radius of the circle ="<<r<<std::endl<<"The area ofthe circle = "<<s<<std::endl;
}
//_cylindrical.h
#ifndef _CYLINDRICAL_H_INCLUDED
#define _CYLINDRICAL_H_INCLUDED
#include "_circle.h"
#include "iostream"
class_cylindrical
{
public:
_cylindrical(double r=0.0,double _height=0.0);
~_cylindrical();;
double volume();
void display();
private:
_circle circle;
double h,_volume;
};
#endif //_CYLINDRICAL_H_INCLUDED
//_cylindrical.cpp
#include "_cylindrical.h"
_cylindrical::_cylindrical(double r,double _height):circle(r)
{
h=_height;
_volume=_cylindrical::volume();
}
_cylindrical::~_cylindrical()
{
std::cout<<"The cylindrical destructor hasbeen called."<<std::endl;
}
double_cylindrical::volume()
{
return circle.area()*h;
}
void_cylindrical::display()
{
std::cout<<_volume<<std::endl;
}
//test.cpp
#include "_cylindrical.h"
#include <cstdio>
#include <windows.h>
int main()
{
freopen("x1.out","w",stdout);
for(float i=1.0;i<=3.0;i+=0.1)
{
{
_cylindrical a(i,10);
a.display();
}
std::cout<<std::endl;
}
std::cout<<std::endl;
for(float i=3.0;i<=5.0;i+=0.1)
{
_circle b(i);
std::cout<<std::endl;
b.display();
}
std::cout<<"\nThe screen will be closed in60 seconds.\n";
Sleep(1000*60);
return0;
}
//题目(2)源代码:
//_stack.h
#ifndef _STACK_H_INCLUDED
#define _STACK_H_INCLUDED
#include <iostream>
typedefint DataType;
class _stack
{
public:
_stack();
~_stack();
bool _is_empty()const;
bool _is_full()const;
bool push(const DataType& data);
bool pop(DataType& data);
void _top();
private:
enum{maxn=10};
DataType num[maxn];
DataType top;
};
#endif //_STACK_H_INCLUDED
//_stack.cpp
#include "_stack.h"
_stack::_stack()
{
top=0;
}
bool _stack::_is_empty()const
{
return top==0;
}
bool _stack::_is_full()const
{
return top==maxn;
}
bool _stack::push(const DataType& data)
{
if(top<maxn)
{
num[top++]=data;
return true;
}
else
return false;
}
bool _stack::pop(DataType& data)
{
if(top>0)
{
data=num[--top];
return true;
}
return false;
}
void _stack::_top()
{
for(int i=0;i<10;i++)
std::cout<<num[i]<<" ";
}
_stack::~_stack()
{
delete []num;
std::cout<<std::endl<<"The destructor has beencalled."<<std::endl;
//std::cout<<num<<std::endl;
}
//test.cpp
#include "_stack.h"
int main()
{
_stack st;
for(int i=0;i<10;i++)
st.push(i+1);
st._top();
for(int i=0;i<10;i++)
st.pop(i);
//std::cout<<std::endl;
//st._top();
//st.~_stack();
//st._top();
return0;
}
//题目(3)源代码:
//version 1,succeed
#include <iostream>
#include <cstdio>
#include <windows.h>
using namespace std;
class exp3
{
public:
exp3(int);
exp3(const exp3&){++obj_num;};
~exp3();
staticint get_obj_num();
int get_number();
private:
staticint obj_num;
int number;
};
int exp3::obj_num =0; //这里的int不能少
exp3::exp3(int n)
{
number=n;
++obj_num;
}
exp3::~exp3()
{
--obj_num;
cout<<"The destructor has beencalled."<<endl;
}
int exp3::get_number()
{
return number;
}
int exp3::get_obj_num()
{
return obj_num;
}
int main()
{
freopen("x1.out","w",stdout);
exp3 obj1(3);
cout<<"obj1.number = "<<obj1.get_number()<<endl;
cout<<"Now obj_num = "<<exp3::get_obj_num()<<endl;
cout<<endl;
exp3 obj2(obj1);
cout<<"obj2.number = "<<obj2.get_number()<<endl;
cout<<"Now obj_num = "<<exp3::get_obj_num()<<endl;
cout<<endl;
exp3 obj3=obj1;
cout<<"obj3.number = "<<obj3.get_number()<<endl;
cout<<"Now obj_num = "<<exp3::get_obj_num()<<endl;
cout<<endl;
exp3 *obj4=new exp3(6);
cout<<"obj4.number = "<<(*obj4).get_number()<<endl;
cout<<"Now obj_num = "<<exp3::get_obj_num()<<endl;
cout<<endl;
delete obj4;
cout<<"Now obj_num = "<<exp3::get_obj_num()<<endl;
printf("***********************************************************\n");
for(int i=0;i<20;i++)
{
exp3 obj5(i+19);
printf("obj%d.number =%d\n",i+5,i+19);
printf("Now obj_num =%d\n",exp3::get_obj_num());
printf("\n");
}
printf("***********************************************************\n");
exp3 *obj6;
for(int i=0;i<20;i++)
{
obj6=new exp3(i+19);
printf("obj%d.number =%d\n",i+25,i+26);
printf("Now obj_num =%d\n",exp3::get_obj_num());
printf("\n");
}
printf("***********************************************************\n");
for(int i=0;i<20;i++)
{
exp3 *obj7=new exp3(i+19);
printf("obj%d.number =%d\n",i+45,i+29);
deleteobj7;
printf("Now obj_num =%d\n",exp3::get_obj_num());
printf("\n");
}
cout<<"The screen will be closed in 60seconds.\n";
Sleep(1000*60);
return0;
}
//version 2,failed
//_stack.h
#ifndef _STACK_H_INCLUDED
#define _STACK_H_INCLUDED
#include <iostream>
typedefint DataType;
class _stack
{
public:
_stack(int);
set_obj_num();
~_stack();
bool _is_empty()const;
bool _is_full()const;
bool push(DataType data);
bool pop(DataType data);
void _top();
static DataType get_obj_num();
//DataType get_obj_num();
private:
//enum{maxn=10};
DataType *data;
DataType num;
DataType _size;
static DataType obj_num;
//DataType obj_num;
};
#endif //_STACK_H_INCLUDED
//_stack.cpp
#include "_stack.h"
_stack::_stack(int s)
{
//set_obj_num();
_size=s;
data=new DataType[_size];
num=0;
//std::cout<<(data+1)<<std::endl;
obj_num++;
}
/*_stack::set_obj_num()
{
obj_num=0;
}*/
bool _stack::_is_empty()const
{
return num==0;
}
bool _stack::_is_full()const
{
return num==_size;
}
bool _stack::push(DataType _data)
{
if(!_is_full())
{
data[num++]=_data;
returntrue;
}
else
returnfalse;
}
bool _stack::pop(DataType _data)
{
if(!_is_empty())
{
_data=data[--num];
returntrue;
}
returnfalse;
}
void _stack::_top()
{
for(int i=0;i<_size;i++)
std::cout<<data[i]<<" ";
}
_stack::~_stack()
{
delete[]data;
std::cout<<std::endl<<"The destructor has beencalled."<<std::endl;
//std::cout<<(data+1)<<std::endl;
obj_num--;
}
DataType _stack::get_obj_num()
{
return obj_num;
//;
}
//test.cpp
#include "_stack.h"
int main()
{
_stack st_1(2);
std::cout<<st_1.get_obj_num()<<std::endl;
_stack st_2(3);
std::cout<<st_2.get_obj_num()<<std::endl;
return0;
}
实验十
1 实验目的:掌握面向对象编程中继承、派生的用法。
2 实验题目:
(1)编写一个集合类,要求类的每个对象可以保存0~100个不同的整数,并使用友元函数实现如下功能:
①从集合中加入一个整数
②从集合中去掉一个整数
③判断一个整数是否在集合中
④求两个集合的并集,结果是一个集合
⑤求两个集合的交集,结果是一个集合
(2)下面是一个形状类Shape,编写Shape的派生类:圆类Circle、三角形类Triangle和矩形类Rectangle,并重定义基类的成员函数使之返回正确的结果(show函数要输出对象的基本信息),然后编写程序进行测试。
class Shape
{public:
double area()
{return 0;};
double girth()
{return 0;};
void show()
{cout<<”Shape Object”<<endl;};
private:
};
(3)编写一个笔类,描述所有笔的共同属性,然后编写笔类的派生类:钢笔、铅笔、签字笔、毛笔,在各派生类中尽量描述清楚各自的属性。
(4)自学完成:定义复数类,重载“+”“-”“=”运算符。
3 实验结果截图
图片2 题目(2)程序运行截图
图片3 题目(3)程序运行截图
图片4 题目(4)程序运行截图
4 源代码
//题目(1)源代码:
//_set.h
#ifndef _SET_H_INCLUDED
#define _SET_H_INCLUDED
#include <iostream>
#include <cstdio>
#include <windows.h>
class _set
{
friend _add(_set &,int);
friend _del(_set &,int);
friendbool _judge(_set &,int);
//friend _set &_union(_set &,_set &);//求并集
//friend _set &_intersection(_set &,_set &);
friend _set_union(_set &,_set &);//求并集
friend _set_intersection(_set &,_set &);//求交集
public:
_set(int);
_set();
~_set();
display(_set &);
private:
int*p;
int _size;//size of number
};
#endif // _SET_H_INCLUDED
// _set.cpp: implementation of the _set class.
//
//////////////////////////////////////////////////////////////////////
#include "_set.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
_set::_set()
{
}
_add(_set &s,int n)
{
s.p[s._size++]=n;
}
_del(_set &s,int n)
{
printf("After deleted%d,the elements of the operated set include:\n",n);
int index;
for(int i=0;i<s._size;i++)
if(s.p[i]==n)
index=i;
for(int j=index;j<s._size;j++)
{
s.p[j]=s.p[j+1];
}
s._size--;
}
bool _judge(_set &s,int n)
{
for(int i=0;i<s._size;i++)
if(s.p[i]==n)
{
printf("%d is in theoperated set.\n",n);
returntrue;
}
printf("%d is not inthe operated set.\n",n);
returnfalse;
}
_set _union(_set &s1,_set &s2)//求并集
//_set _union(_set &s1,_set &s2)//求并集
{
_set s3(s1._size+s2._size);
for(int i=0;i<s1._size;i++)
s3.p[s3._size++]=s1.p[i];
for(int j=0;j<s2._size;j++)
{
int index=0;
for(;index<s2._size;index++)
if(s3.p[index]==s2.p[j])
//continue;
break;
if(index==s1._size)
{
s3.p[s3._size++]=s2.p[j];
}
}
std::cout<<"The union of sets:"<<std::endl;
return s3;
}
_set _intersection(_set &s1,_set &s2)
//_set _intersection(_set &s1,_set &s2)
{
_set s3(s1._size+s2._size);
int bigger;
if(s1._size>s2._size)
bigger=s1._size;
else
bigger=s2._size;
for(int i=0;i<bigger;i++)
{
for(int j=0;j<bigger;j++)
{
if(s1.p[i]==s2.p[j])
s3.p[s3._size++]=s1.p[i];
}
}
std::cout<<"The intersection of sets:"<<std::endl;
return s3;
}
_set::~_set()
{
delete[]p;
std::cout<<"The destruction has beencalled"<<std::endl;
}
_set::_set(int n)
{
p=newint[n];
_size=0;
}
_set::display(_set &s)
{
for(int i=0;i<s._size;i++)
{
std::cout<<s.p[i]<<" ";
if((i+1)%4==0)
std::cout<<std::endl;
}
}
//test.cpp
#include "_set.h"
int main() //测了几次感觉没毛病
{
int n;
std::cin>>n;
_set s1(n),s2(n);
for(int i=0;i<n;i++)
{
_add(s1,i);
_add(s2,i+3);
}
std::cout<<"s1:\n";
s1.display(s1);
std::cout<<std::endl;
std::cout<<std::endl;
std::cout<<"s2:\n";
s2.display(s2);
std::cout<<"\n\n";
_set s3=_union(s1,s2);
s3.display(s3);
std::cout<<"\n\n";
_set s4=_intersection(s1,s2);
s4.display(s4);
std::cout<<"\n\n";
_del(s1,8);
s1.display(s1);
std::cout<<"\n\n";
_judge(s1,8);
std::cout<<"\n";
_judge(s2,5);
std::cout<<"The screen will be closed in60s."<<std::endl;
Sleep(1000*60);
return0;
}
//题目(2)源代码:
//Shape.h
#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED
#include <iostream>
//#include <windows.h>
class Shape
{
public:
Shape();
virtual~Shape();
double area(){return0;};
double girth(){return0;};
virtualvoid show(){ std::cout<<"Shape Object"<<std::endl;};
protected:
double _girth,_area;
};
#endif //SHAPE_H_INCLUDED
// Shape.cpp: implementation of the Shape class.
//
//////////////////////////////////////////////////////////////////////
#include "Shape.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Shape::Shape()
{
}
Shape::~Shape()
{
}
//Circle.h
#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED
#include "Shape.h"
#define pi 3.1415926
class Circle:public Shape
{
public:
Circle();
Circle(double);
virtual~Circle();
double area();
double girth();
void show();
private:
double r;
};
#endif //CIRCLE_H_INCLUDED
// ircle.cpp: implementation of the Circle class.
//
//////////////////////////////////////////////////////////////////////
#include "Circle.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Circle::Circle()
{
}
Circle::Circle(double R)
{
r=R;
}
Circle::~Circle()
{
std::cout<<"The circle destruction has beencalled."<<std::endl;
}
double Circle::area()
{
//_area=pi*r*r;
return pi*r*r;
}
void Circle::show()
{
_area=area();
_girth=girth();
std::cout<<"It is a circle object."<<std::endl;
std::cout<<"The radius of the circle is"<<r<<std::endl;
std::cout<<"The area of the circle is"<<_area<<std::endl;
std::cout<<"The girth of circle is "<<_girth<<std::endl;
}
double Circle::girth()
{
//_girth=2*pi*r;
return2*pi*r;
}
//Rectangle.h
#ifndef RECTANGLE_H_INCLUDED
#define RECTANGLE_H_INCLUDED
//#include <windows.h>
#include "Shape.h"
classRectangle:public Shape
{
public:
Rectangle();
Rectangle(double,double);
~Rectangle();
double area();
double girth();
void show();
private:
double length,width;
};
#endif // RECTANGLE_H_INCLUDED
//Rectangle.cpp
#include "Rectangle.h"
Rectangle::Rectangle()
{
}
Rectangle::Rectangle(double _a,double _b)
{
length=_a;
width=_b;
}
Rectangle::~Rectangle()
{
std::cout<<"The rectangle destruction hasbeen called."<<std::endl;
}
voidRectangle::show()
{
_girth=girth();
_area=area();
std::cout<<"It is a rectangle object."<<std::endl;
std::cout<<"The length and width of therectangle are "<<length<<" "<<width<<std::endl;
std::cout<<"The area of the rectangle is"<<_area<<std::endl;
std::cout<<"The girth of the rectangle is"<<_girth<<std::endl;
}
doubleRectangle::area()
{
return length*width;
}
doubleRectangle::girth()
{
return2*(length+width);
}
//Triangle.h
#ifndef TRIANGLE_H_INCLUDED
#define TRIANGLE_H_INCLUDED
#include "Shape.h"
#include <cmath>
//#include <windows.h>
classTriangle:public Shape
{
public:
Triangle();
Triangle(double,double,double);
virtual~Triangle();
double area();
double girth();
void show();
bool judge();
private:
double _side1,_side2,_side3;
};
#endif //TRIANGLE_H_INCLUDED
// Triangle.cpp: implementation of the Triangle class.
//
//////////////////////////////////////////////////////////////////////
#include "Triangle.h"
#include <math.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Triangle::Triangle()
{
}
Triangle::Triangle(double _a,double _b,double _c)
{
_side1=_a;
_side2=_b;
_side3=_c;
}
Triangle::~Triangle()
{
std::cout<<"The triangle destruction hasbeen called."<<std::endl;
}
doubleTriangle::area()
{
if(judge())
{
double s=0.0,l=(_side1+_side2+_side3)/2;
s=sqrt(l*(l-_side1)*(l-_side2)*(l-_side3));
return s;
}
return0.0;
}
doubleTriangle::girth()
{
if(judge())
return _side1+_side2+_side3;
return0.0;
}
voidTriangle::show()
{
if(judge())
{
_area=area();
_girth=girth();
std::cout<<"It is atriangle object."<<std::endl;
std::cout<<"The threesides of the triangle are "<<_side1<<" "<<_side2<<" "<<_side3<<" "<<std::endl;
std::cout<<"The area ofthe triangle is "<<_area<<std::endl;
std::cout<<"The girth oftriangle is "<<_girth<<std::endl;
}
else
std::cout<<"It is not alawful triangle."<<std::endl;
}
boolTriangle::judge()
{
//abc acb bac bca cab cba
if(_side1+_side2>_side3 && _side1+_side3>_side2 && _side2+_side1>_side3 && _side2+_side3>_side1 && _side3+_side1>_side2 &&_side3+_side2>_side1)
returntrue;
returnfalse;
}
//test.cpp
#include "Circle.h"
#include "Triangle.h"
#include "Rectangle.h"
/*
为什么不能在main.cpp或者其他的的头文件中添加 <windows.h>,否则第28到第31行(Rectangle 相关的)会报错?
*/
int main()
{
Circle c1(1.0);
c1.show();
std::cout<<std::endl;
Triangle t1(1.0,2.0,3.0);
t1.show();
std::cout<<std::endl;
Triangle t2(10.0,3.0,8.0);
t2.show();
std::cout<<std::endl;
std::cout<<std::endl;
Triangle t3(3.0,4.0,5.0);
t3.show();
std::cout<<std::endl;
Rectangle r1(1.0,2.0),r2(3.0,5.0);
r1.show();
std::cout<<std::endl;
r2.show();
std::cout<<std::endl;
//std::cout<<"The screenwill be closed in 60s."<<std::endl;
//Sleep(1000*60);
return0;
}
//题目(3)源代码:
//writing_instruments.h
#ifndef WRITING_INSTRUMENTS_H_INCLUDED
#define WRITING_INSTRUMENTS_H_INCLUDED
#include <iostream>
#include <string>
#include <windows.h>
classwriting_instruments
{
protected:
double price;
bool used_in_exam;
bool me_owned_;
double service_span;
std::string place_of_origin;
int num;
public:
writing_instruments();
writing_instruments(int);
virtual~writing_instruments();
virtualvoid show(){std::cout<<"..."<<std::endl;}
virtualvoid add(int);
virtualvoid _minus(int);
};
#endif //WRITING_INSTRUMENTS_H_INCLUDED
//writing_instruments.cpp
#include "writing_instruments.h"
writing_instruments::writing_instruments()
{
price=0.0;
used_in_exam=false;
me_owned_=false;
service_span=0.0;
place_of_origin="xxxxxxxxxxxxxxxxx";
num=0;
}
writing_instruments::~writing_instruments()
{
std::cout<<"The writing_instrumentsdestructor has been called."<<std::endl;
}
writing_instruments::writing_instruments(int n)
{
price=0.0;
used_in_exam=false;
me_owned_=false;
service_span=0.0;
place_of_origin="xxxxxxxxxxxxxxxxx";
num=n;
}
void writing_instruments::add(int n)
{
num+=n;
}
voidwriting_instruments::_minus(int n)
{
num-=n;
}
//pen.h
#ifndef PEN_H_INCLUDED
#define PEN_H_INCLUDED
#include "writing_instruments.h"
class pen:public writing_instruments
{
private:
std::string nib;
std::string colour;
//enumcolour{Red,Orange,Yellow,Green,Blue,Indigo,Violet};
std::string kind;
public:
pen(int,std::string,std::string);
virtual~pen();
void show();
void add(int);
void _minus(int);
};
#endif //PEN_H_INCLUDED
//pen.cpp
#include "pen.h"
pen::pen(int n,std::string _colour,std::string _nib)
{
price=10;
nib=_nib;
num=n;
colour=_colour;
price=100.0;
used_in_exam=true;
me_owned_=true;
service_span=10.0;
place_of_origin="China";
}
pen::~pen()
{
std::cout<<"The pen destructor has beencalled."<<std::endl;
}
void pen::add(int n)
{
num+=10*n;
}
void pen::_minus(int n)
{
num-=100*(-n);
}
void pen::show()
{
add(10);
_minus(20);
std::cout<<"It is a pen object."<<std::endl;
std::cout<<"Here are "<<num<<" pen "<<"."<<std::endl;
std::cout<<"The nib of the pen is "<<nib<<"."<<std::endl;
std::cout<<"The colour of the pen is "<<colour<<"."<<std::endl;
std::cout<<"The kind of the pen is "<<kind<<"."<<std::endl;
std::cout<<"The price of the pen is "<<price<<"."<<std::endl;
std::cout<<"The pen can serve you for"<<service_span<<" years."<<std::endl;
std::cout<<"The pen was made in "<<place_of_origin<<"."<<std::endl;
std::cout<<"You can use it inexamination"<<"."<<std::endl;
std::cout<<"Some pens owned."<<std::endl;
}
//pencil.h
#ifndef PENCIL_H_INCLUDED
#define PENCIL_H_INCLUDED
#include "writing_instruments.h"
class pencil:public writing_instruments
{
private:
std::string colour;
std::string apply_to;
std::string XB;
public:
pencil(int,std::string,std::string);
virtual~pencil();
void show();
void add(int);
void _minus(int);
};
#endif //PENCIL_H_INCLUDED
//pencil.cpp
#include "pencil.h"
pencil::pencil(int n,std::string _colour,std::string _XB)
{
price=10;
num=n;
colour=_colour;
XB=_XB;
price=1.0;
used_in_exam=true;
me_owned_=true;
service_span=0.3;
place_of_origin="China";
apply_to="students&&workmen&&white_collar&&etc";
}
pencil::~pencil()
{
std::cout<<"The pencil destructor has beencalled."<<std::endl;
}
void pencil::add(int n)
{
num+=120*n;
}
void pencil::_minus(int n)
{
num-=100*(-2*n);
}
void pencil::show()
{
add(10);
_minus(20);
std::cout<<"It is a pencil object."<<std::endl;
std::cout<<"Here are "<<num<<" pencils"<<"."<<std::endl;
std::cout<<"The colour of the pencil is"<<colour<<"."<<std::endl;
std::cout<<"The price of the pencil is"<<price<<" yuan."<<std::endl;
std::cout<<"The pencil can serve you for"<<service_span<<" years."<<std::endl;
std::cout<<"The pencil was made in "<<place_of_origin<<"."<<std::endl;
std::cout<<"You can use it inexamination"<<"."<<std::endl;
std::cout<<"Some pencils owned."<<std::endl;
}
//sign_pen.h
#ifndef SIGN_PEN_H_INCLUDED
#define SIGN_PEN_H_INCLUDED
#include "writing_instruments.h"
classsign_pen:public writing_instruments
{
private:
std::string colour;
std::string apply_to;
double nib_diameter;
public:
sign_pen(int,double,std::string);
virtual~sign_pen();
void show();
void add(int);
void _minus(int);
};
#endif //SIGN_PEN_H_INCLUDED
//sign_pen.cpp
#include "sign_pen.h"
sign_pen::sign_pen(int n,double _nib_diameter,std::string _colour)
{
nib_diameter=_nib_diameter;
price=10;
num=n;
colour=_colour;
price=1.0;
used_in_exam=true;
me_owned_=true;
service_span=2.0;
place_of_origin="China";
apply_to="students&&workmen&&white_collar&&etc";
}
sign_pen::~sign_pen()
{
std::cout<<"The sign_pen destructor hasbeen called."<<std::endl;
}
voidsign_pen::add(int n)
{
num+=n*n+2;
}
voidsign_pen::_minus(int n)
{
num-=(n*n)/2;
}
voidsign_pen::show()
{
add(109);
_minus(20);
std::cout<<"It is a sign_pen object."<<std::endl;
std::cout<<"Here are "<<num<<" sign pens"<<"."<<std::endl;
std::cout<<"The colour of the sign pen is"<<colour<<"."<<std::endl;
std::cout<<"The price of the sign pen is"<<price<<" yuan."<<std::endl;
std::cout<<"The diameter of the sign pennib "<<nib_diameter<<" mm."<<std::endl;
std::cout<<"The sign pen can serve you for"<<service_span<<" years."<<std::endl;
std::cout<<"The sign pen was made in "<<place_of_origin<<"."<<std::endl;
std::cout<<"You can use it inexamination"<<"."<<std::endl;
std::cout<<"Some sign pens owned."<<std::endl;
}
//writing_brushes.h
#ifndef WRITING_BRUSHES_H_INCLUDED
#define WRITING_BRUSHES_H_INCLUDED
#include "writing_instruments.h"
#include <cmath>
classwriting_brushes:public writing_instruments
{
private:
std::string property;
std::string person_apply_to;
std::string apply_to_what;
std::string colour;
public:
writing_brushes(int,std::string,std::string,std::string);
virtual~writing_brushes();
void show();
void add(int);
void _minus(int);
};
#endif //WRITING_BRUSHES_H_INCLUDED
//writing_brushes.cpp
#include "writing_brushes.h"
writing_brushes::writing_brushes(int n,std::string _colour,std::string _property,std::string_apply_to_what)
{
price=10;
property=_property;
num=n;
colour=_colour;
price=255.0;
used_in_exam=false;
me_owned_=false;
service_span=2.0;
place_of_origin="China";
person_apply_to="students&&workmen&&white_collar&&etc";
apply_to_what=_apply_to_what;
}
writing_brushes::~writing_brushes()
{
std::cout<<"The writing_brushes destructorhas been called."<<std::endl;
}
voidwriting_brushes::add(int n)
{
num+=sqrt(n)*20-1;
}
voidwriting_brushes::_minus(int n)
{
num-=n*sqrt(n)+2;
}
voidwriting_brushes::show()
{
add(109);
_minus(19);
std::cout<<"It is a writing_brushesobject."<<std::endl;
std::cout<<"Here are "<<num<<" writing brushes"<<"."<<std::endl;
std::cout<<"The colour of the writing brushis "<<colour<<"."<<std::endl;
std::cout<<"The price of the writing brushis "<<price<<" yuan."<<std::endl;
std::cout<<"This kind of brush is suitablefor "<<apply_to_what<<"."<<std::endl;
std::cout<<"This property of writing brushis "<<property<<"."<<std::endl;
std::cout<<"The writing brush can serve youfor "<<service_span<<" years."<<std::endl;
std::cout<<"The writing brush was made in"<<place_of_origin<<"."<<std::endl;
std::cout<<"Generally You can't use it inexamination"<<"."<<std::endl;
std::cout<<"No writing brush owned."<<std::endl;
}
//test.cpp
#include "pen.h"
#include "pencil.h"
#include "sign_pen.h"
#include "writing_brushes.h"
int main()
{
pen p1(10,"Black","Steel");
p1.show();
std::cout<<std::endl;
pencil pc1(100,"Red","2B");
pc1.show();
std::cout<<std::endl;
sign_pen sgp1(20,0.5,"Purple");
sgp1.show();
std::cout<<std::endl;
writing_brushes wb1(25,"Black","hard","drawing");
wb1.show();
std::cout<<std::endl;
std::cout<<"The screen will be closed in60s."<<std::endl;
Sleep(1000*60);
return0;
}
//题目(3)源代码:
//_complex_number.h
#ifndef _COMPLEX_NUMBER_H_INCLUDED
#define _COMPLEX_NUMBER_H_INCLUDED
#include <iostream>
#include <cmath>
class _complex_number
{
private:
double re;
double im;
public:
_complex_number();
_complex_number(double,double);
~_complex_number();
void display(_complex_number &);
_complex_number operator+(_complex_number &)const;
_complex_number operator-(_complex_number &)const;
//_complex_number operator=(_complex_number &);//为什么这里用引用做参数就不行呢?
_complex_number operator=(_complex_number );
};
#endif // _COMPLEX_NUMBER_H_INCLUDED
//_complex_number.cpp
#include "_complex_number.h"
_complex_number::_complex_number()
{
}
_complex_number::_complex_number(double _re,double _im)
{
re=_re;
im=_im;
}
_complex_number::~_complex_number()
{
std::cout<<"The destructor has beencalled."<<std::endl;
}
/*
注意,在运算符表示法中,运算符左侧的对象是调用对象,运算符右边的对象是作为参数被传递的对象。
*/
_complex_number _complex_number::operator+(_complex_number &c1)const
{
_complex_number c2;
c2.re=re+c1.re;
c2.im=im+c1.im;
return c2;
}
_complex_number _complex_number::operator-(_complex_number &c1)const
{
_complex_number c2;
c2.re=re-c1.re;
c2.im=im-c1.im;
return c2;
}
_complex_number _complex_number::operator=(_complex_number c1)//有时需要考虑自赋值的特殊情况
{
//_complex_number c2;
re=c1.re;
im=c1.im;
return*this;
}
void_complex_number::display(_complex_number &c)
{
if(c.im>0)//为什么这里写成 if(c.im>0) 输出与预期不符?
std::cout<<c.re<<" + "<<c.im<<"i"<<std::endl;
else
std::cout<<c.re<<" - "<<std::abs(c.im)<<"i"<<std::endl;
}
//test.cpp
#include "_complex_number.h"
int main()
{
_complex_number c1(1.0,2.0),c2(3.0,5.0),c3,c4;
c1.display(c1);
c2.display(c2);
c3=c1-c2;
c4=c1+c2;
c3.display(c3);
c4.display(c4);
Sleep(1000*60);
std::cout<<"The screen will be closed in60s."<<std::endl;
return0;
}
这是一个有情怀的链接:https://pan.baidu.com/s/1luf0dXpSSc4uk-kiPnn9gw 密码:tajm