1、C+实验报告蔺子杰 10021225李世斌 10021229实验一一、实验目的1、了解在系统上如何编辑、编译、连接和运行一个 C+程序;2、运行简单的 C+程序,初步了解 C+源程序的结构、特点;3、掌握 C+中输入/输出、函数重载、引用的概念及用法。二、实验原理在 C 语言中,函数名必须是唯一的。C+支持函数(名)重载,即在同一个作用域内多个函数可以共用一个函数名。被重载的函数称为重载函数。函数重载要求:重载函数至少在参数个数或参数类型上与已定义的函数不同。例如:square ( int i );square ( float i );square ( double i );三、实验内容1.
2、编一个程序,对任意输入的两个数据进行求和运算,数据类型可以 int 型 float 型。int sum( int, int);float sum( float, float);2. 任意输入三个数,将这三个数由大到小输出四、实验结果内容 1 的截图 内容 2 的截图五、源代码 #include “stdafx.h“#include “iostream.h“int Sum(int num1,int num2);float Sum(float num1,float num2);void P1();void P2();void Compare(float int main(int argc, cha
3、r* argv)int choice=0;int isCorrect=0;while(isCorrect=0)coutchoice;switch(choice)case 1:P1();isCorrect=1;break;case 2:P2();isCorrect=1;break;default :coutnum1;coutnum2;num11=(int)num1;num12=(int)num2;if(num11num1;cinnum2;cinnum3;float float float Compare(num11,num12,num13);void Compare(float if(a=b)i
4、f(b“=a)temp=c;c=a;a=temp;cout“=a)temp=a;a=b;b=temp;cout“length;coutwidth;coutheight;int Rectangle:computeVolume()volume=length*width*height;return volume;/ project2.cpp : Defines the entry point for the console application./#include “stdafx.h“#include “ioStream.h“#include “rectangle.h“int main()Rect
5、angle r1;r1.Set_Data();puteVolume();r1.display();Rectangle r2;r2.Set_Data();puteVolume();r2.display();Rectangle r3;r3.Set_Data();puteVolume();r3.display();return 0;2. 内容 2 的源代码 class Student public:Student(int StuNum,int StuScore);void display();void Set_Data();private:int num; int score;/char name2
6、0;/char sex;#include “student.h“#include “iostream.h“Student:Student(int StuNum,int StuScore)num=StuNum;score=StuScore;void Student:display()cout ”运算符来调用类 A 中的私有成员。在 C 语言中,函数名必须是唯一的。C+支持函数(名)重载,即在同一个作用域内多个函数可以共用一个函数名。被重载的函数称为重载函数。函数重载要求:重载函数至少在参数个数或参数类型上与已定义的函数不同。例如:square ( int i );square ( float i
7、 );square ( double i );三、实验内容1.理解源程序中友元函数的使用;将本程序中的 Time 类声明为 Date 类的友元类,通过Time 类中的 display 函数引用 Date 类对象的私有数据,输出年、月、日和时、分、秒。2.声明一个复数类 Complex,重载运算符“+” 、 “” 、 “”、 “”,使之能用于复数的加、减、乘、除,运算符重载函数作为 Complex 类的成员函数。编程序,分别求两个复数之和差积商。四、实验结果内容 1 的截图内容 2 的截图五、源代码1.内容 1 的源代码#include using namespace std;class Dat
8、e;class Time public:Time(int,int,int);void display(Date private:int hour;int minute;int sec; ;class Date friend class Time;public:Date(int,int,int);private:int month;int day;int year;Time:Time(int h,int m, int s) hour=h;minute=m;sec=s;void Time:display(Date double impart;public:Complex(double r=0,do
9、uble i=0)repart=r;impart=i;Complex operator + (Complex c.repart=repart+c1.repart;c.impart=impart+c1.impart;return c;Complex operator - (Complex c.repart=repart-c1.repart;c.impart=impart-c1.impart;return c;Complex operator * (Complex c.repart=repart*c1.repart-impart*c1.impart;c.impart=repart*c1.impar
10、t+impart*c1.repart;return c;Complex operator / (Complex c.repart=(repart*c1.repart+impart*c1.impart)/(c1.repart*c1.repart+c1.impart*c1.impart);c.impart=(impart*c1.repart-repart*c1.impart)/(c1.repart*c1.repart+c1.impart*c1.impart);return c;void display()coutconst double PI=3.1415926;class Shape publi
11、c:virtual double area()=0;class Circle:public Shape public:Circle(double r=0)radius=r;double area();protected:double radius;class Square:public Shape public:Square(double l=0)a=l;double area();protected:double a;class Rectangle:public Shape public:Rectangle(double l=0,double w=0)length=l;width=w;dou
12、ble area();protected:double length,width;class Trapezoid:public Shape public:Trapezoid(double s=0,double l=0,double h=0)shortbottom=s;longbottom=l;height=h;double area();protected:double shortbottom,longbottom,height;class Triangle:public Shape public:Triangle(double b=0,double h=0)bottom=b;height=h
13、;double area();protected:double bottom,height;double Circle:area()return PI*radius*radius;double Square:area()return a*a;double Rectangle:area()return length*width;double Trapezoid:area()return (shortbottom+longbottom)*height/2;double Triangle:area()return bottom*height/2;int main() double sum=0;Cir
14、cle c(12.6);Square s(3.5);Rectangle r(4.5,8.4);Trapezoid tra(2.0,4.5,3.2);Triangle tri(4.5,8.4);Shape *p;p=coutarea()area();p=coutarea()area();p=coutarea()area();p=coutarea()area();p=coutarea()area();cout#include int iflaw(double a,double b,double c)double temp;if(ab)if(bc);else temp=b;b=c;c=temp;else temp=a;a=b;b=temp;if(bc);else temp=b;b=c;c=temp;if(ab);else temp=a;a=b;b=temp;if(b+c)=b) coutabc;s=iflaw(a,b,c);if(s=2)continue;elsearea=calculate(a,b,c);coutarea;break;