当前位置: 首页 > 范文大全 > 公文范文 >

[面向对象程序设计基础r实验指导和实验报告年]面向过程程序设计

时间:2021-10-15 13:23:04 来源:网友投稿

《面向对象程序设计基础R》实验指导和实验报告(年)

 ———————————————————————————————— 作者:

 ———————————————————————————————— 日期:

 

 实验报告?   实践报告□

 课程名称:  面向对象程序设计基础R  

 实验、实践名称:  面向对象程序设计基础R

 

 实验、实践地点: 逸夫楼201    

 专业班级: 软件1601      学号:      

 学生姓名:    

   

 指导教师: 宋春花

 2017年 4月  17

 日

 实验名称

 实验一 熟悉Visual Studio 开发环境

 实验目的和要求

 (1) 熟悉基本的输入输出方法;

 (2) 掌握函数的定义,调用和声明方法,函数的参数传递机制,函数嵌套调用和递归调用,内联函数,带默认形参的函数,重载函数;

 (3) 理解命名空间的概念,掌握命名空间的使用;

 (4) 熟悉const关键字的使用;

 (5) 掌握内存的动态分配的概念和使用;

 (5) 掌握类的定义和对象的定义和使用;

 实验内容

 1.编写重载函数area()和perimeter(),分别计算圆、长方形、正方形的面积和周长,并在主函数中测试之。

 2.完善程序,并上机运行:(此程序见原模板)

 3.定义一个复数类Complex,复数的实部Real与虚部Image定义为私有数据成员。用复数类定义复数对象c1、c2、c3,用默认构造函数将c1初始化为c1=20+40i,将c2初始化为c2=0+0i,用拷贝构造函数将c3初始化为c3=20+40i。用公有成员函数Display()显示复数c1、c2与c3的内容。

 4. 定义一个矩形类Rectangle,矩形的左上角(Left,Top)与右下角坐标(Right,Bottom)定义为保护数据成员。用公有成员函数Diagonal()计算出矩形对角线的长度,公有成员函数Show()显示矩形左上角与右下角坐标及对角线长度。在主函数中用new运算符动态建立矩形对象r1,初值为(10,10,20,20)。然后调用Show()显示矩形左上角与右下角坐标及对角线长度。最后用delete运算符回收为矩形动态分配的存储空间。

 主要仪器设备

 台式或笔记本电脑:1台/人

 实验记录(写出实验内容中1、2、3、4的程序代码和运行结果)(可分栏或加页)

 1.#include "stdafx.h"

 #include"iostream"

 using namespace std;

 const double pi=3.141592;

 double area(double r);

 double area(double a,double b);

 double perimer(double r);

 double perimer(double a,double b);

 double area(double r)

 {

 ?double s;

 ?s=pi*r*r;

 ?return s;

 }

 double area(double a,double b)

 {

 ?double s;

  s=a*b;

  return s;

 }

 double perimer(double r)

 {

 ?double p;

 ?p=2*pi*r;

  return p;

 }

 double perimer(double a,double b)

 {

  double p;

  p=2*(a=b);

  return p;

 }

 int _tmain(int argc, _TCHAR* argv[])

 {

  double r;

 ?double m;

  double n;

  cout<<"请输入圆的半径:"<<endl;

 ?cin>>r;

 ?cout<<"圆的面积为:"<<area(r)<<" "<<"圆的周长为:"<<perimer(r)<<endl;

 ?cout<<"请输入长方形的长和宽:"<<endl;

  cin>>m>>n;

 ?cout<<"长方形面积为:"<<area(m,n)<<" "<<"长方形的周长为:"<<perimer(m,n)<<endl;

 ?cout<<"请输入正方形的边长:"<<endl;

  cin>>m;

  cout<<"正方形的面积为:"<<area(m,m)<<" "<<"正方形的周长为:"<<perimer(m,m)<<endl;

 ?getchar();getchar();

  return 0;

 }

 // 实验1-2.cpp : 定义控制台应用程序的入口点。

 //

 #include"stdafx.h"

 #include "iostream"

 using namespace std;

 const double pi = 3.14;

 class Circle {

 public:

  double area() { return x*x*pi; }

  Circle(double a) :x(a) { }

 private:double x;

 };

 class Rectangle {

 public:

  Rectangle(double x,double y):len(x),wid(y){}//写出矩形类的构造函数

 ?double area() { return len*wid; }//写出计算矩形面积函数

 private:

  double len, wid;

 };

 class Square {

 public:

 ?Square(double x) :len(x) { }//写出正方形类的构造函数

 ?double area() { return len*len; }//写出计算正方形面积函数

 private:

 ?double len;

 };

 int main()

 {

 ?cout << "Input shape" << endl;

 ?cout << "if circle, input c, if rectangle input r; if square input s" << endl;

 ?char shape;

 ?cin >> shape;

 ?switch (shape)

  {

  case 'c': {  double r;

  ?cout << "input radius" << endl;

 ? cin >> r;

 ? Circle r1(r);

 ? cout << " circle area=" << r1.area() << endl;

  system("pause");

 ? break;

  }

 ?case 'r': { double len, wid;

 cout << "input length and width" << endl;

 ? cin >> len >> wid;

 Rectangle jx(len,wid); //构造矩形对象,宽为wid,高为len 

  ?cout << "rectangle area=" << jx.area() << endl;//输出矩形面积

  system("pause");

 break;

  }

 ?case 's': {  double len;

 ? cout << "input length" << endl;

 cin >> len;

 ? Square fx(len);//构造正方形对象,边长为len

  cout << "square area=" << fx.area() << endl;//输出正方形面积

  ?system("pause");

 break;

  }

 ?default: cout << "input error!" << endl;

 break;

 ?}

 ?return 0;

 }

 3. #include "stdafx.h"

 #include"iostream"

 using namespace std;

 class Complex

 {

 public:

  Complex(double a,double b):real(a),image(b){};

 ?Complex(Complex &c);

 ?void Dispaly();

 

 private :

  double real;

 ?double image;

 };

 Complex::Complex(Complex & c)

 {

  real = c.real;

 ?image = c.image;

 }

 void Complex::Dispaly()

 {

  cout <<"(" <<real << "+" << image << "i"<<")"<< endl;

 }

 int main()

 {

 ?Complex c1(20,40);

  Complex c2(0, 0);

  Complex c3(c1);

  c1.Dispaly(); 

  c2.Dispaly();

  c3.Dispaly();

 ?system("pause");

 }

 4. #include "stdafx.h"

 #include"iostream"

 #include"math.h"

 using namespace std;

 class Rectangle{

 protected:

  double Left,Top,Right,Bottom;

 public:

  Rectangle(double a,double b,double c,double d):Left(a),Top(b),Right(c),Bottom(d){};

  void Show(Rectangle &d);

 ?double Diagonal(Rectangle &c);

 };

 double Rectangle::Diagonal(Rectangle &c){

 ?double x=c.Right-c.Left;

 ?double y=c.Top-c.Bottom;

 ?double d=sqrt((x*x+y*y));

 ?return d;

 }

 void Rectangle::Show(Rectangle &d){

 

  cout<<"左 上 角 "<<"("<<Left<<","<<Top<<")"<<endl;

  cout<<"右 下 角 "<<"("<<Right<<","<<Bottom<<")"<<endl;

  cout<<"斜 角 线 长 度¨¨"<<d.Diagonal(d)<<endl;

 }

 int main(){

  Rectangle *r1=new Rectangle(10,10,20,20);

  r1->Show(*r1);

   delete r1;

  system("pause");

  return 0;

 }

 遇到的问题和解决方法

 心得体会

 ?

 实验名称

 实验二 类与对象的特性

 实验目的和要求

 (1) 掌握类的定义和对象的定义和使用;

 (2) 掌握静态数据成员和静态成员函数的定义和使用方法;

 (3) 理解类的作用域、对象的作用域及生存周期;

 (4) 掌握函数调用中参数的传递;

 (5) 掌握常量类型;

 (6) 掌握友元函数和友元类的定义及使用。

 实验内容

   编写一个学生类。(1)输出每个学生的姓名、学号、成绩;(2)统计并输出学生的总人数、总成绩、平均成绩、最高成绩、最低成绩。

 主要仪器设备

 台式或笔记本电脑:1台/人

 实验记录(写出实验内容中程序代码和运行结果)(可分栏或加页)

 #include"stdafx.h"

 #include <iostream>

 using namespace std;

 //定义学生类

 class Student{

  int no;   

  //学号

  char name[10];    //姓名

 ?double score; //成绩

 ?static int totalNumber; //学生人数

 ?static double totalScore; //总成绩

  static double lowestScore;  //最低成绩

  static double highestScore;  //最高成绩

 public:

  Student(int no_,char *name_,double score_); //构造函数

 ?static void Output(); //输出相关信息

 void StudentInformation();  //输出学生基本信息

 };

 int Student::totalNumber=0;    //静态数据初始化

 double Student::highestScore =0.;

 double Student::lowestScore =100.;

 int main( )

 { Student stu1(1001,"张小三¨y",97.5);

 ?stu1.StudentInformation ();

 ?Student stu2(1625,"李 老 四",83.);

  stu2.StudentInformation ();

  Student stu3(1628,"王 老 五",93.);

 ?stu3.StudentInformation ();

  Student stu4(1352,"郭 小 六¨′",62.5);

  stu4.StudentInformation ();

 ?Student stu5(1999,"王 小 明",77.);

 ?stu5.StudentInformation ();

 ?Student::Output ();

   ?system("pause");

  return 0;

 }

 Student::Student (int no_,char *name_,double score_)

 { no=no_;

 ?strcpy(name,name_);

 ?score=score_;

  totalNumber++;

 ?totalScore+=score;

 ?if (score>highestScore)

  ?highestScore=score;

 ?if (score<lowestScore)

  ?lowestScore=score;

 }

 void Student::StudentInformation ()

 {  cout<<"学 号"<<no<<"  "<<"姓 名 "<<name<<" "<<"成 绩:"<<score<<endl;

 }

 void Student::Output ()

 {?cout<<"学 总 数 "<<totalNumber<<"  "<<"总

 成  绩 "<<totalScore<<"

  "<<endl;

  cout<<"平 均 成 绩"<<totalScore/totalNumber<<" "<<"最 高 成 绩 "<<highestScore<<"

 "<<"最 低 成 绩"<<lowestScore<<"

  "<<endl;

 }

 遇到的问题和解决方法

 心得体会

 实验名称

 实验三 继承与派生

 实验目的和要求

 (1)掌握类的继承和派生概念;

 (2)掌握派生类的定义与使用;

 (3)掌握派生类的构造函数与析构函数的应用及调用顺序;

 (4)理解赋值兼容原则的应用。

 实验内容

 考察一个点、圆、圆柱体的层次结构,计算圆和圆柱体的面积,阅读程序并运行。

 建立3个类,分别为点类、圆类、圆柱类,点类派生得到圆类,圆类派生得到圆柱类。

 主要仪器设备

 台式或笔记本电脑:1台/人

 实验记录(写出实验内容中的程序代码和运行结果)(可分栏或加页)

 #include "stdafx.h"

 #include <iostream>

 #include<iomanip>

 using namespace std;

 class Point {

  friend ostream &operator<< (ostream &, const Point &);

 public:

 ?Point(int = 0, int = 0);?// 带默认参数的构造函数

  void setPoint(int, int);?// 对点坐标数据赋值

  int getX() const { return x; }

 ?int getY() const { return y; }

 protected:

  int x, y;

  ?// Point类的数据成员

 };

 class Circle : public Point {

  friend ostream &operator<< (ostream &, const Circle &);?// 友元函数

 public:

 ?Circle(double r = 0.0, int x = 0, int y = 0); // 构造函数

 ?void setRadius(double);  //置半径  

 ?double getRadius() const;   //返回半径

 ?double area() const;// 返回面积

 protected:   double radius;?// 数据成员,半径

 };

 class Cylinder :public Circle {

  friend ostream & operator<<(ostream &, const Cylinder &);

  // 友元函数

 public:

  Cylinder(double h = 0.0, double r = 0.0, int x = 0, int y = 0);   // 构造函数

 ?void setHeight(double);  // 置高度值

 ?double getHeight() const;    //返回高度值

 ?double area() const;?    //返回面积      

 ?double volume() const;?  //返回体积

 protected:

 ?double height; // 数据成员,高度

 };

 Point::Point(int a, int b) // 构造函数,调用成员函数对 x,y作初始化

 {

  setPoint(a, b);

 }

 void Point::setPoint(int a, int b) { x = a; y = b; }// 对数据成员置值

 ostream &operator<< (ostream &output, const Point &p) // 重载插入运算符,输出对象数据

 {

 ?output << '[' << p.x << "," << p.y << "]";

  return output;

 }

 // 带初始化式构造函数,首先调用基类构造函数

 Circle::Circle(double r, int a, int b) : Point(a, b) { setRadius(r); }

 void Circle::setRadius(double r) { radius = (r >= 0 ? r : 0); }// 对半径置值

 double Circle::getRadius() const { return radius; }// 返回半径值

 double Circle::area() const { return 3.14159 * radius * radius; }// 计算并返回面积值

 ostream & operator<< (ostream &output, const Circle &c) // 输出圆心坐标和半径值

 {

 ?output << "Center = " << '[' << c.x << "," << c.y << "]" << "; Radius = "

  << setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << c.radius;

  return  output;

 }

 // 带初始化式构造函数,首先调用基类构造函数

 Cylinder::Cylinder(double h, double r, int x, int y) :Circle(r, x, y) { setHeight(h); }

 void Cylinder::setHeight(double h) { height = (h >= 0 ? h : 0); }// 对高度置值

 double Cylinder::getHeight() const { return height; }// 返回高度值

 double Cylinder::area() const { return

 2 * Circle::area() + 2 * 3.14159*radius*height; }// 计算并返回圆柱体的表面积

 double Cylinder::volume() const { return  Circle::area()*height; }// 计算并返回圆柱体的体积

  ? ? // 输出数据成员圆心坐标、半径和高度值

 ostream &operator<< (ostream &output, const Cylinder &cy)

 {

 ?output << "Center = " << '[' << cy.x << "," << cy.y << "]" << "; Radius = "

  << setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << cy.radius

  ?<< "; Height = " << cy.height << endl;

  return output;

 }

 #include <iostream>

 #include <iomanip>

 using namespace std;

 int main()

 {

 ?Point p(72, 115); ?//定义点对象并初始化

 ?cout << "The initial location of p is " << p << endl;

  p.setPoint(10, 10);//置点的新数据值

 ?cout << "\nThe new location of p is " << p << endl;?//输出数据

  Circle c(2.5, 37, 43); //定义圆对象并初始化

  cout << "\nThe initial location and radius of c are\n" << c << "\nArea = " << c.area() << "\n";

 ?c.setRadius(4.25); c.setPoint(2, 2); //置圆的新数据值

  //输出圆心坐标和圆面积

 ?cout << "\nThe new location and radius of c are\n" << c << "\nArea = " << c.area() << "\n";

  Cylinder cyl(5.7, 2.5, 12, 23);?//定义圆柱体对象并初始化

 ? //输出圆柱体各数据和表面积,体积

  cout << "\nThe initial location, radius and height of cyl are\n" << cyl

  ?<< "Area = " << cyl.area() << "\nVolume = " << cyl.volume() << '\n';

 ?//置圆柱体的新数据值

  cyl.setHeight(10);   cyl.setRadius(4.25);

  cyl.setPoint(2, 2);

 ?cout << "\nThe new location, radius and height of cyl are\n" << cyl

 ? << "Area = " << cyl.area() << "\nVolume = " << cyl.volume() << "\n";

  system("pause");

 }

 遇到的问题和解决方法

 心得体会

 ?

 实验名称

 实验四 多态性

 实验目的和要求

 (1)掌握C++中运算符重载的机制和运算符重载的方式;

 (2)理解类型转换的必要性,掌握类型转换的使用方法;

 (3)理解多态性,掌握虚函数的设计方法;

 (4)掌握纯虚函数和抽象类的使用方法。

 实验内容

 某小型公司,主要有三类人员:管理人员、计时人员和计件人员。现在,需要存储这些人的姓名、编号、时薪、工时、每件工件薪金、工件数,计算月薪并显示全部信息。

 设计4个类,分别为雇员类(为抽象类)、管理人员类、计时人员类和计件人员类,其中管理人员类、计时人员类和计件人员类由雇员类派生得到。

 主要仪器设备

 台式或笔记本计算机:1台/人

 实验记录(写出实验内容中的程序代码和运行结果) (可分栏或加页)

 #include "stdafx.h"

 #include"iostream"

 #include"iomanip"

 #include"string.h"

 using namespace std;

 //Employee.h

 class Employee

 {    

  //雇员类—-抽象类

 public:

 ? Employee(  int,const char*name_);

 ?virtual ~Employee();? //虚析构函数

  string getName() ;

   long getNumber() ;

 ?virtual double earnings(double) ;?//纯虚函数,计算月薪

  virtual void print() ;

  //虚函数,输出编号、姓名

 protected:

 ?long number; // 编号

  string name; ?// 姓名

 };

 // Manager.h

 class Manager : public Employee { //管理人员类

 public:

 ?const Manager( int,const  char*name_, double );

 ?~Manager() { }

 ?void setMonthlySalary(double);  // 置月薪

 ?virtual double earnings() ;  // 计算管理人员月薪

  virtual void print() ; // 输出管理人员信息

 private:

  double monthlySalary;? //私有数据,月薪

 };

 // HourlyWorker.h

 class HourlyWorker : public Employee {   //计时人员类

 public:

 ? HourlyWorker(int, const  char*name_, double, int);

  ~HourlyWorker() {}

 ?void setWage(double); ?   //置时薪

 ?void setHours(int);  

  //置工时

 ?virtual double earnings() ;   // 计算计时工月薪

 ?virtual void print() ;?

  // 输出计时工月薪

 private:

  double wage;  //时薪

 ?double hours; //工时

 };

 // PieceWorker.h

 class PieceWorker : public Employee {   //计件人员类

 public:

  PieceWorker(int, const char*name_, double , int);

  ~PieceWorker() { }

 ?void setWage(double);//置每件工件薪金

 ?void setQuantity(int);//置工件数

  virtual double earnings() ; // 计算计件薪金

 ?virtual void print() ;    // 输出计件薪金

 private:

 ?double wagePerPiece;?//每件工件薪金

  int quantity; //工件数

 };

 int main()

 {

  cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2);

 ?Manager m1(10135, "Cheng ShaoHua", 1200);

 ?HourlyWorker hw1(30712, "Zhao XiaoMing", 5, 8 * 20);

 ?PieceWorker pw1(20382, "Xiu LiWei", 0.5, 2850);

  // 使用抽象类指针,调用派生类版本的函数

 ?Employee *basePtr;

  basePtr = &m1; basePtr->print();

  basePtr = &hw1;   basePtr->print();

 ?basePtr = &pw1;  basePtr->print();

 ?system("pause");

 }

 Employee::Employee( int nu_, const  char*name_)

 {

 ?number = nu_;

  name = name_;

 }

 Employee::~Employee()

 {

 }

 string Employee::getName() 

 {

 ?return name;

 }

  long Employee::getNumber()

 {

  return number;

 }

  double Employee::earnings(double pay)

 {return pay;

  }

 void Employee::print() {

 }

 Manager::Manager(int nu_,const char*name_, double pay):Employee(nu_,name_), monthlySalary(pay)

 {}

 void Manager::setMonthlySalary(double pay)

 {monthlySalary = pay;

 }

 double Manager::earnings()

 {return monthlySalary;

 }

 void Manager::print() 

 {cout<< "管理者编号:" << number <<"\n管理者月薪" << monthlySalary << "\n";

 }

 HourlyWorker::HourlyWorker(int nu_, const char*name_, double pay, int hour):Employee(nu_,name_),wage(pay),hours(hour)

 {

 }

 void HourlyWorker::setWage(double pay)

 {wage = pay;

 }

 void HourlyWorker::setHours(int hour)

 {hours = hour;

 }

 double HourlyWorker::earnings()

 {return wage*hours;

 }

 void HourlyWorker::print()

 {

  cout << "计时员编号:" << number << "计时员月薪" << wage*hours << endl;

 }

 PieceWorker::PieceWorker(int nu_, const char*name_, double pay, int piece):Employee(nu_,name_), wagePerPiece(pay), quantity(piece)

 {}

 void PieceWorker::setWage(double pay)

 {wagePerPiece = pay;

 }

 void PieceWorker::setQuantity(int piece)

 {quantity = piece;

 }

 double PieceWorker::earnings()

 {return wagePerPiece*quantity;

 }

 void PieceWorker::print()

 {cout << "计件员编号" << number << "计件员计件薪" << wagePerPiece*quantity << endl;}

 遇到的问题和解决方法

 心得体会

相关热词搜索: 实验 程序设计 面向对象 指导 基础