我的代码世界
来源:互联网 发布:壹号人家女装批发淘宝 编辑:程序博客网 时间:2024/04/29 15:24
目录
第八章(第七次上机作业)
第八章(第七次上机作业)...1
1.编写一个日期类MyDate,实现...1
前置、后置++和--运算...1
日期+整数,日期-整数,整数+日期...1
日期-日期...1
日期比较(>、<、==、!=)...1
1.MyDate.h.1
2.MyDate.cpp.3
3.main.cpp.10
4.截图结果...14
2.编写一个程序,计算正方体cube、球体sphere和圆柱体cylinde的表面积和体积。(要求:抽象出一个公共基类Shape作为虚基类,运用虚函数的知识设计程序...15
1.shape.h.15
2.shape.cpp.16
3. cube.h.17
4.cube.cpp.17
5. sphere.h.19
6. sphere.cpp.20
7. cylinde.h.22
8. cylinde.cpp.23
9.main.cpp.24
10.运行截图...27
1.编写一个日期类MyDate,实现
前置、后置++和--运算
日期+整数,日期-整数,整数+日期
日期-日期
日期比较(>、<、==、!=)
1.MyDate.h
#pragmaonce
#ifndef MYDATE_H
#defineMYDATE_H
#include<iostream>
usingnamespace std;
classMyDate
{
public:
MyDate(int year = 0, int month = 0, int day = 0);
MyDate& operator ++ ();//前置单目运算符重载
MyDate& operator -- ();//前置单目运算符重载
MyDate operator ++(int);//后置单目运算符重载
MyDate operator --(int);//后置单目运算符重载
bool operator > (constMyDate& mydate);//大于运算符重载
bool operator <(constMyDate& mydate);//小于运算符重载
bool operator ==(constMyDate& mydate);//等于运算符重载
bool operator !=(constMyDate& mydate);//不等于运算符重载
friendMyDate operator + (constMyDate &mydate,constint a); //运算符+重载,日期加整数
friendMyDate operator -(constMyDate &mydate,constint a); //运算符-重载,日期减整数
friendMyDate operator + (constint a,constMyDate &mydate ); //运算符+重载,整数加日期
friendMyDate operator - (constMyDate &mydate7, constMyDate &mydate8); //运算符-重载,日期-日期
void ShowMyDate()const;
friendostream&operator<< (ostream &out, constMyDate &mydate9);//重载输出流运算符
~MyDate();
private://私有数据成员
int Year;
int Month;
int Day;
};
#endif
2.MyDate.cpp
#include"stdafx.h"
#include"MyDate.h"
#include<iostream>
usingnamespace std;
MyDate::MyDate(int year ,int month ,intday ) : Year(year), Month(month), Day(day)
{
cout << "我的日期类初始化完成" << endl;
}
MyDate&MyDate::operator++()
{
Day++;
if (Day > 30)
{
Day -= 30;
Month++;
if (Month > 12)
{
Month -= 12;
Year++;
}
}
return *this;
}
MyDate&MyDate::operator--()
{
MyDate date;
if (Day >= 1 && Month >= 1 && Year>= 1)
{
date.Day = Day--;
date.Month = Month--;
date.Year = Year--;
return *this;
}
else
{
date.Day = 30;
date.Month = 12;
date.Year = 1;
return *this;
}
}
MyDateMyDate::operator++(int)
{
MyDate date = *this;
++(*this);
return date;
}
MyDateMyDate::operator--(int)
{
MyDate date = *this;
--(*this);
return date;
}
boolMyDate::operator>(constMyDate&mydate)
{
if ((Year -mydate.Year > 0) || (Year -mydate.Year == 0 && Month -mydate.Month > 0) || (Month -mydate.Month&&Day -mydate.Day > 0))
{
returntrue;
}
returnfalse;
}
boolMyDate::operator<(constMyDate&mydate)
{
if ((Year -mydate.Year < 0) || (Year -mydate.Year == 0 && Month -mydate.Month < 0) || (Month -mydate.Month==0&&Day -mydate.Day < 0))
{
returntrue;
}
returnfalse;
}
boolMyDate::operator==(constMyDate&mydate)
{
if ((Year -mydate.Year == 0) && (Month -mydate.Month == 0) && (Day -mydate.Day == 0))
{
returntrue;
}
returnfalse;
}
boolMyDate::operator!=(constMyDate&mydate)
{
if ((Year -mydate.Year != 0) || (Year -mydate.Year == 0 && Month -mydate.Month != 0) || (Month -mydate.Month == 0 && Day -mydate.Day != 0))
{
returntrue;
}
returnfalse;
}
MyDate::~MyDate()
{
cout << "我的日期类清理完成" << endl;
}
voidMyDate::ShowMyDate()const
{
cout << "年:" << Year<< "\t" << "月:" << Month<< "\t" << "日:" << Day<< "\t" << endl;
}
MyDate operator + (constMyDate &mydate,constinta)
{
returnMyDate(mydate.Year + a,mydate.Month +a,mydate.Day +a);
}
MyDate operator - (constMyDate &mydate,constinta)
{
returnMyDate(mydate.Year - a,mydate.Month -a,mydate.Day -a);
}
MyDate operator + (constinta,constMyDate &mydate)
{
returnMyDate(a +mydate.Year,a+mydate.Month,a+mydate.Day);
}
MyDate operator - (constMyDate &mydate7,constMyDate &mydate8)
{
returnMyDate(mydate7.Year - mydate8.Year,mydate7.Month -mydate8.Month,mydate7.Day -mydate8.Day);
}
ostream & operator<< (ostream &out,constMyDate &mydate9)
{
out <<"(" <<mydate9.Year <<"," <<mydate9.Month<<","<<mydate9.Day<<")";
returnout;
}
3.main.cpp
//运算符重载.cpp :定义控制台应用程序的入口点。
//
#include"stdafx.h"
#include"MyDate.h"
#include<iostream>
#include<conio.h>
usingnamespace std;
int_tmain(intargc,_TCHAR*argv[])
{
MyDate MYDATE1(2014,12,30);
cout << "当前的日期为:" << endl;
MYDATE1.ShowMyDate();
system("pause");
_getch();
MyDate *MYdate;
MYdate = &MYDATE1;
++MYDATE1;
cout << "测试前置自加:" << endl;
MYdate->ShowMyDate();//测试前置自加
_getch();
--MYDATE1;
cout << "测试前置自减:" << endl;
MYdate->ShowMyDate();//测试前置自减
_getch();
MYDATE1++;
cout << "测试后置自加:" << endl;
MYdate->ShowMyDate();//测试后置自加
_getch();
MYDATE1--;
cout << "测试后置自减:" << endl;
MYdate->ShowMyDate();//测试后置自减
_getch();
MyDate MYDATE2(2015, 5, 20);
if (MYDATE2> MYDATE1)//大于运算符重载测试
{
cout <<"大于运算符重载测试" << endl;
}
_getch();
MyDate MYDATE3(2013, 5, 20);
if (MYDATE3 < MYDATE2)//小于运算符重载测试
{
cout << "小于运算符重载测试" << endl;
}
_getch();
MyDate MYDATE4(2013, 5, 20);
if (MYDATE4 == MYDATE3)//等于运算符重载测试
{
cout << "等于运算符重载测试" << endl;
}
_getch();
MyDate MYDATE5(2013, 5, 21);
if (MYDATE5 != MYDATE4)//不等于运算符重载测试
{
cout << "不等于运算符重载测试" << endl;
}
int a = 1;
MyDate MYDATE6(2014,5,20);
MyDate MYDATE7 = MYDATE6 -1;
cout << "MYDATE6 - 1" << MYDATE7<< endl;//日期减整数
_getch();
MYDATE7= MYDATE6 + 1;
cout << "MYDATE6 +1" << MYDATE7<< endl;//日期加整数
_getch();
MYDATE7 = 1+MYDATE6 ;
cout << "1+MYDATE6 " << MYDATE7<< endl;//整数加日期
_getch();
MYDATE7 = MYDATE5 - MYDATE6;
cout << "MYDATE5-MYDATE6 " << MYDATE7<< endl;//整数加整数
_getch();
system("pause");
return 0;
}
4.截图结果
2.编写一个程序,计算正方体cube、球体sphere和圆柱体cylinde的表面积和体积。(要求:抽象出一个公共基类Shape作为虚基类,运用虚函数的知识设计程序
1.shape.h
#pragmaonce
#ifndef SHAPE_H
#defineSHAPE_H
classshape
{
public:
shape();
virtual void display() ;//虚函数就是可以通过基类的指针访问派生类的相同成员
virtual ~shape();
};
void Function(shape*Shape);
#endif
2.shape.cpp
#include"stdafx.h"
#include"shape.h"
#include<iostream>
usingnamespace std;
shape::shape()
{
cout << "基类初始化完成" << endl;
}
voidshape::display()
{
cout <<"基类shape的显示函数display" << endl;
}
shape::~shape()
{
cout << "基类清理完成" << endl;
}
void Function(shape*Shape)
{
Shape->display();
}
3. cube.h
#pragmaonce
#include"shape.h"
#ifndef CUBE_H
#defineCUBE_H
classcube :
virtual publicshape
{
public:
cube(float length=0);
float CubeArea();//面积
float CubeVolume();//体积
void display() ;
virtual~cube();
private:
float Length;
};
#endif
4.cube.cpp
#include"stdafx.h"
#include"cube.h"
#include<iostream>
#include<cmath>
usingnamespace std;
cube::cube(floatlength) :Length(length)
{
cout << "棱初始化完成" << endl;
}
cube::~cube()
{
cout << "棱清理完成" << endl;
}
voidcube::display()
{
cout << "正方体"<< endl;
cout << "棱长:" << Length<< endl;
cout << "派生类cube的display函数完成" << endl;
}
floatcube::CubeArea()
{
cout << "正方体面积"<< endl;
return (pow(Length,2)*6);
}
floatcube::CubeVolume()
{
cout << "正方体体积"<< endl;
return (pow( Length,3));
}
5. sphere.h
#pragmaonce
#include"shape.h"
#ifndef SPHERE_H
#defineSPHERE_H
classsphere :
virtual publicshape//虚基类的含义就是无论有多少派生类都只保存一份副本,只调用基类相同的函数
{//而不调用派生类的相同函数
public:
sphere(float radius);
float SphereArea();//面积
float SphereVolume();//体积
void display() ;
virtual~sphere();
private:
float Radius;
};
#endif
6. sphere.cpp
#include"stdafx.h"
#include"sphere.h"
#include<iostream>
#include<cmath>
usingnamespace std;
sphere::sphere(floatradius) :Radius(radius)
{
cout << "圆的半径初始化完成" << endl;
}
sphere::~sphere()
{
cout << "半径清理完成" << endl;
}
voidsphere::display()
{
cout << "球"<< endl;
cout << "半径:" <<Radius<< endl;
cout << "派生类sphere的display函数完成" << endl;
}
floatsphere::SphereArea()
{
cout << "面积:" << endl;
return (4 * 3.14*pow(Radius, 2));
}
floatsphere::SphereVolume()
{
cout << "体积:" << endl;
return (1.0 / 4.0 * 3 * 3.14*pow(Radius,3));
}
7. cylinde.h
#pragmaonce
#include"shape.h"
#ifndef CYLINDE_H
#define CYLINDE_H
classcylinde :
virtual publicshape
{
public:
double CylindeArea();//面积
double CylindeVolume();//体积
cylinde(double radiuS,double heighT);
void display() ;
virtual~cylinde();
private:
double RAdius;
double HEight;
};
#endif
8. cylinde.cpp
#include"stdafx.h"
#include"cylinde.h"
#include<iostream>
#include<cmath>
usingnamespace std;
cylinde::cylinde(doubleradiuS,doubleheighT) :RAdius(radiuS), HEight(heighT)
{
cout << "圆柱的半径和高初始化完成" << endl;
}
cylinde::~cylinde()
{
cout << "圆柱的半径和高清理完成" << endl;
}
voidcylinde::display()
{
cout <<"圆柱" << endl;
cout << "圆柱的半径:" << RAdius<< endl;
cout << "圆柱的高:" << HEight<< endl;
cout << "派生类sphere的display函数完成" << endl;
}
doublecylinde::CylindeArea()
{
cout << "面积:" << endl;
return (3.14*RAdius*HEight + 3.14*pow(RAdius,2));
}
doublecylinde::CylindeVolume()
{
cout << "体积:" << endl;
return (3.14*pow(RAdius, 2)*HEight);
}
9.main.cpp
//虚函数.cpp :定义控制台应用程序的入口点。
//
#include"stdafx.h"
#include"shape.h"
#include"cube.h"
#include"sphere.h"
#include"cylinde.h"
#include<iostream>
#include<cmath>
#include<conio.h>
usingnamespace std;
int_tmain(intargc,_TCHAR*argv[])
{
system("cls");
shape shape1;
shape *shape2;
shape2 = &shape1;
Function(shape2);//基类虚函数使用
shape2->display();
_getch();//基类的使用
system("cls");
cube cube1(3);
cube *cube2 = &cube1;
Function(cube2);//派生类cube的虚函数使用
cout << cube2->CubeArea() <<endl;
cout << cube2->CubeVolume() <<endl;
_getch();
system("cls");
sphere sphere1(3);
sphere *sphere2=&sphere1;
Function(sphere2);//派生类sphere
cout << sphere2->SphereArea()<< endl;
cout << sphere2->SphereVolume()<< endl;
_getch();
system("cls");
cylinde cylinde1(5, 5);
cylinde *cylinde2;
cylinde2 = &cylinde1;
Function(cylinde2);//派生类虚函数使用指针实现
cout << cylinde2->CylindeArea()<< endl;
cout << cylinde2->CylindeVolume()<< endl;
_getch();
system("cls");
cylinde cylinde3(5,5);
cylinde3.display();//虚基类的使用
system("pause");
return 0;
}
10.运行截图
- 我的代码世界
- 我的代码世界
- 编程世界,我的世界。
- 我的Java世界
- 我的世界
- 我的程序世界
- 我的世界
- 我眼中的世界
- 我的电影世界
- 我的世界
- OpenGL我的世界
- 我的完美世界
- 我生活的世界
- 我的太极世界
- 我的云世界
- 我的世界
- 《我看见的世界》
- 我的世界
- 《C++ Primer第四版》 第二章 变量和基本类型 - 总结
- 3岁孩子亦会不自信吗[图]
- 最大熵学习笔记(五)最优化算法
- 最大熵学习笔记(六)优缺点分析
- 致 高 山
- 我的代码世界
- 最大熵学习笔记(零)目录和引言
- XCode 打包问题巧遇
- 妹妹想哥哥
- hibernate异常
- 最新CAX/EDA/CFD/GIS/光学/化工/液压软件资源网
- iOS 关于 设计模式 与网友讨论实录
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况,在方法调用前
- S1 商品信息管理系统