类与对象(二)

更新时间:2024-05-30 03:00:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

《C++面向对象程序设计》实验报告

实验3 类与对象(二)

[实验目的]

1、进一步加深对类和对象的理解。

2、掌握类的构造函数和析构函数的概念和使用方法。 3、掌握对象的数组、对象的指针及其使用方法。

4、掌握定义静态数据成员和静态成员函数的方法及使用。 5、掌握友元的概念和使用。

[实验要求]

给出以下各实验内容的源程序代码,并把编译、运行过程中出现的问题以及解决方法填入实验报告中,按时上交。 [实验学时] 2学时。

[实验内容]

1、分析下面程序结果,了解有参构造函数、无参构造函数、析构函数的定义和调用过程,并了解对象数组的使用方法。 #include class Chap { private:

int i; public:

Chap(int x); // 有参构造函数 Chap( ); // 无参构造函数 ~Chap(); // 析构函数 };

Chap::Chap(int x) { i=x;

cout<<\}

Chap::Chap( ) {

cout<<\}

Chap:: ~ Chap( ) {

cout<<\}

int main()

第 1 页

《C++面向对象程序设计》实验报告

{ Chap array1[2]={3,5},array2[2]; cout<<\return 0; }

回答问题:

① 程序的运行结果。

Constructor with parameter called. 3 Constructor with parameter called. 5 Constructor with no parameter called. Constructor with no parameter called. Exit main.

Destructor called. -858993460 Destructor called. -858993460 Destructor called. 5 Destructor called. 3

② 程序运行情况分析。

建立两个Chap类的数组,每个数组有两元素,给第一个数组的元素分别赋初值3和5,第二个元素不赋初值,则第一个数组调用有参构造函数,第二个数组调用无参构造函数。Main函数运行结束,对象的生命周期结束,调用析构函数,并遵循“先构造的后析构,后构造的先析构”。

2、对象数组及对象指针应用。

建立一个对象数组,内放5个学生的数据(学号、1门成绩)。利用指针,统计平均成绩并输出第1,3,5个学生的数据。 [源程序]

#include using namespace std;

class Student {

public: int number; int score; void get_info(); };

void Student::get_info()

第 2 页

《C++面向对象程序设计》实验报告

{ cin>>number>>score; }

void main() { Student std[5]; Student *p; int i,sum=0; p=&std[0]; cout<<\请输入学生信息:\ for(i=0;i<5;i++) { p->get_info(); p++; } p=&std[0]; for(i=0;i<5;i++) { sum+=p->score; p++; } float ave; ave=float(sum)/5; cout<<\平均成绩:\ p=&std[0]; for(i=0;i<5;i+=2) { cout<<\第\个学生的学号\,成绩\ p+=2; } }

3、用静态数据成员和静态成员函数设计程序。

商店销售某一商品,商店每天公布统一的折扣(discount),同时允许销

第 3 页

《C++面向对象程序设计》实验报告

售人员在销售时灵活掌握售价(对一次购10件以上者,还可以享受9.8折优惠)。现已知当天3个销售员销售情况为:

销售员号(num) 销货件数(quantity) 销售均价(price)

101 5 23.5 102 12 24.56 103 100 21.5

请编程序,计算出当日此商品的总销售款sum及每件商品的平均售价。

提示:将折扣discount、总销售款sum和商品销售总件数n声明为静态数据

成员,再定义静态成员函数average(求平均售价)和display(输出结果)。 [源程序]

#include using namespace std;

class Product { private: int num; int quantity; float price; static double discount; static double sum; static int n; public: Product(int n,int q,double p); void total(); static double average(); static void display(); };

Product::Product(int n,int q,double p) { num=n; quantity=p; price=p; }

void Product::total()

第 4 页

《C++面向对象程序设计》实验报告

{ double rate=1.0; if(quantity>10) rate=0.98*rate; sum=sum+quantity*price*rate*(1-discount); n=n+quantity; }

void Product::display() { cout<double Product::average() { return(sum/n); }

double Product::discount=0.05; double Product::sum=0; int Product::n=0;

int main() {

Product

Prod[3]={Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5)}; for(int i=0;i<3;i++) Prod[i].total(); Product::display(); return 0; }

4、设计程序。

已知点类Point, 包括两个数据成员:x(横坐标),y(纵坐标);若干成员函数。其中计算两点间距离的函数distance( ),分别采用以下两种方法设计。

① 将distance( )作为Point类的成员函数。

② 将distance( )作为Point类的友元函数。(distance( )是类外的一个普

第 5 页

《C++面向对象程序设计》实验报告

通函数)

注:以点(0,0)和(3,4)作为测试数据,求出它们之间的距离。 ①

[源程序]

#include #include using namespace std;

class Point {

private: float x,y; public: Point(float a,float b){x=a;y=b;}; void distance(Point p); };

void Point::distance(Point p) { float distance; distance=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y)); cout<<\两点间距离为:\}

void main() { Point p1(0,0),p2(3,4); p1.distance(p2); } ②

[源程序]

#include #include

class Point {

private:

第 6 页

《C++面向对象程序设计》实验报告

double x,y; public: Point(double a,double b){x=a;y=b;}; friend void distance(Point p1,Point p2); };

void distance(Point p1,Point p2) { double distance; distance=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); cout<<\两点间距离为:\}

void main() { Point p1(0,0),p2(3,4); distance(p1,p2); }

第 7 页

本文来源:https://www.bwwdw.com/article/92c6.html

Top