面向对象程序设计试验_第1页
面向对象程序设计试验_第2页
面向对象程序设计试验_第3页
面向对象程序设计试验_第4页
面向对象程序设计试验_第5页
免费预览已结束,剩余20页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验一 C+ 基础1.1 实验目的1了解并熟悉开发环境,学会调试程序;2熟悉 C+ 中简单的标准输入输出函数的使用方法;3理解 const 修饰符的作用并学会应用;4理解内联函数的优缺点并学会其使用场合; 5理解并学会函数重载;6理解并熟练掌握使用 new 和 delete 来分配内存; 7理解并熟练掌握引用的使用方法。1.2 实验内容1.2.1 程序阅读1理解下面的程序并运行,然后回答问题。#include <iostream.h>int max_def(int x, int y)return (x>y?x:y);int max_def(int x, int y, int

2、z)int temp = 0;return (temp=(x>y?x:y)>z?temp:z;double max_def(double x, double y)return (x>y?x:y);int main()int x1 = 0;int x2 = 0;double di = 0.0;double d2 = 0.0;x1 = max_def(5,6);x2 = max_def(2,3,4);di = max_def(2.1,5.6);d2 = max_def(12.3,3.4,7.8);cout<v"x1="vvx1vve ndl;cout&l

3、t;v"x2="vvx2vve ndl;cout<<"d1="<<d1<<e ndl;cout<<"d2="<<d2<<endl;return 1;问题一:上述程序的输出结果是什么?问题二:用的是哪个函数?答:调用的函数是int max_def(i nt x, int y, int z) _int temp = 0;retur n (temp=(x>y?x:y)>z?temp:z; 问题三:处的输出结果为什么是d2=12,而不是d2=12.3 ?答:因

4、为处调用的是整型函数,d2在此函数中被转换为整型,小数点后面被删除。2理解下面的程序并运行,然后回答问题。#i nclude <iostream.h>int main() int *p1int *p2= new int; = new int(0); char *p3 = new char10; return 1;问题一:、处动态申请内存分别代表什么意思?答:new动态分配存放一个整数的内存空间,并将其首地址赋给指针变量pl ;new动态分配存放一个整数的内存空间, 并对其初始化赋值为 0,并将其首地址赋给指针变量 p2 ;new 动态分配存放 10个字符型数组元素的内存空间,并将其

5、首地址赋给指针变量p3。问题二:该程序存在什么不合理的地方?。答:程序结束时没有将分配的空间释放,应该使用delete函数释放内存。3理解下面的程序并运行,然后回答问题。#include <iostream.h> void swap(int a, int b)int temp = a;a = b;b = temp;void swap(int *a, int *b)int temp = *a;*a = *b;*b = temp;int main() int i = 5; int j = 10; cout<<"Before swap: i="<&l

6、t;i<<",j="<<j<<endl; swap(i,j); cout<<"After the first swap: i="<<i<<",j="<<j<<endl; swap(&i,&j); cout«"After the sec ond swap: i="vvivv",j="vvjvve ndl; return 1;问题一:输出结果是什么?Bef oi*e suap:

7、1=5, J =10tei* the f irst swap: i=5 , j =10 Ftei* tlie second suap: i"10,j =5 Press any kev to continue问题二:处函数调用不能实现两个数的交换,而可以,原因是什么?答:处调用的函数只是交换了局部变量a和b,并没有改变i和j的值;处调用的函数使用了引用形参,i和j的值随着此处调用的函数中a和b的对换而对换。问题三:处调用的是哪个函数?答:调用的函数是void swap(i nt a, int b) int temp = a;a = b;b = temp;1.2.2程序设计1 定义两个重

8、名函数,分别求出两点间平面距离和空间距离。#in clude<iostream>#in clude<cmath>using n amespace std;int dista nce(i nt x1,i nt y1 ,int x2,i nt y2)double dis;dis=sqrt(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);cout<<dis<<e ndl;return dis;3int distance(int x1,int y1,int x2,int y2,int z1,int z2)double dis;dis=sqr

9、t(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2); cout<<dis<<endl;return dis;void main()int a;int i,j,k,l,q,w,e,r,t,y;cout<<" 请输入平面两点坐标: "<<endl;cin>>i>>j>>k>>l;a=distance(i,j,k,l);cout<<" 请输入空间两点坐标 "<<endl;cin>>q&

10、gt;>w>>e>>r>>t>>y;a=distance(q,w,e,r,t,y);2.设计一个函数:exch(),当调用exch (a,b,c)时,将a赋值给b, b赋值给c, c赋值给a,要 求采用引用的方式来实现。#include<iostream>#include<cmath>using namespace std;void exch(int &m,int &n,int &p)int temp=p;p=n;n=m;m=temp;int main()int a=1,b=2,c=3; co

11、ut<<"a="<<a<<"b="<<b<<"c="<<c<<endl;exch(a,b,c);cout<<"a="<<a<<"b="<<b<<"c="<<c<<endl;return 0;1.3 思考题1自己设计一个程序,测试指向常量的指针,常指针,指向常量的常指针之间的区别。#include <ios

12、tream> using namespace std;void main()int a = 10;int const *p = &a; cout<<a<<endl; cout<<*p<<endl;int b = 20;我们可以改变指针变量 p 所指向的内容,而不能改变 p 的地址空间,如 添加上 p = &b; 我们就 会发现编译错误! 指向常量的指针 const int*p ,特点是指针所保存的地址可以改变, 然而 指针所指向的值却不可以改变。同理,当添加 *p = b 时,会发生编译错误!指向常量的常指针 const i

13、nt const*p 特点是指针所保存的地址不可变,指针所指向的数值也不可变。2编写一个函数,实现两个字符串变量的交换。#include <iostream> using namespace std;void Exchg2(char *m,char *n) char tmp = *m;*m = *n;*n = tmp;void main()char a = 'q'char b = 'p'cout<<"a="<<a<<" b="<<b<<endl;Exc

14、hg2(&a, &b); cout<<"a="<<a<<" b="<<b<<endl;5实验三 类和对象 构造函数与析构函数3.1 实验目的1理解 this 指针的作用和用法; 2掌握构造函数的定义和作用; 3掌握构造函数的使用; 4掌握拷贝构造函数的定义和使用; 5掌握构造函数的重载; 6掌握析构函数的定义和使用。3.2 实验内容1理解下面的程序并运行,然后回答后面的问题。#include <iostream.h>class CPointpublic:void Se

15、t(int x,int y);void Print();private:int x;int y;void CPoint:Set(int x,int y)x = x;y = y;void CPoint:Print()cout<<"x="<<x<<",y="<<y<<endl;void main()CPoint pt;pt.Set(10,20);pt.Print(); 问题一:以上程序编译能通过吗?如果不能,原因是什么? 能通过编译。: Set(int m,int问题二:以上程序的运行结构是否正确,

16、如果不正确,分析为什么,如何改正? 结果不正确,因为 Set 函数中的形参与类中的相同产生错误,改为 void CPoint : n)。2理解下面的程序并运行,然后回答后面的问题。#include <iostream.h>class CPersonpublic:void Print();private:CPerson();private:int age;char *name;CPerson:CPerson()void CPerson:Print() cout<<"name="<<name<<",age="&

17、lt;<age<<endl;void main()CPerson ps(23,张三"); ps.Print(); 问题一:以上程序存在三个错误,在不改变主函数内容的前提下,试改正该程序。 #include<iostream>#include<string>using namespace std;class CPersonpublic:void Print();CPerson(int m,string n)age=m;name=n;private:int age;string name;void CPerson:Print() cout<

18、<"name="<<name<<",age="<<age<<endl;void main()CPerson ps(23,张三");ps.Print();3.2.2 程序设计1设计实现一个 CPoint 类,满足以下要求:Print()a. 该类包含两个整型成员变量 x (横坐标)和y (纵坐标),以及一个输出函数 用来输出横坐标和纵坐标,要求不可以在类的外部直接访问成员变量;7b. 可以采用没有参数的构造函数初始化对象,此时的成员变量采用默认值0;c. 可以采用直接输入参数的方式来初始化该类

19、的成员变量;#in clude<iostream>#in cludevioama nip>using n amespace std;class CPoi ntpublic:void prin t();CPoi nt()x=O;y=O;poi nt(i nt x1,i nt y1);int GetX() return x;int GetY() return y;private:int x;int y;void CPoi nt:pri nt()8山<<乂<<$6上可(6)<<<<6 ndl;CPoi nt:poi nt(i nt x

20、1, int y1)x=x1;y=y1;void mai n()CPoi nt p;CPoi nt();p.pri nt();p.poi nt(1,2);p.pri nt();p.GetX();p.GetX();3.3 思考题1设计一个 CStudent (学生)类,并使 CStudent类具有以下特点:a. 有学生姓名、学号、程序设计、信号处理、数据结构三门课程的成绩;b. 全部信息由键盘输入;c. 通过成员函数统计学生平均成绩,当课程数量增加时,成员函数无须修改仍可以求取 平均成绩;d. 输出学生的基本信息、各科成绩与平均成绩;e. 学生对象用链表存储; f .统计不及格学生人数。#inc

21、lude<iostream.h> #include<iomanip.h>#include<string.h>#define N 3#define M 3class CStudentpublic:void setstudent(char *name,char *sn,float scoreN);void showstudent();private:char Sname10;char Sno8;float Score3;float Avg;float Sum;int count;void CStudent : setstudent(char *name,char

22、 *sn,float scoreN)int i;float Sum=0.0;int count=0;strcpy(Sname,name);strcpy(Sno,sn);for(i=0;i<N;i+)Scorei=scorei;count+;for(i=0;i<3;i+)Sum=Sum+Scorei;Avg=Sum/count;void CStudent :showstudent()int i;cout<<Sname<<setw(16)<<Sno<<setw(10);for(i=0;i<3;i+) cout<<Scor

23、ei<<setw(10);cout<<setw(12)<<Avg<<endl;void main()int i,j,k=0;char name10,no8;float scoreN;for(j=1;j<=M;j+)cout<<"please input student"<<j<<" name "<<setw(6);cin>>name;cout<<"please input student"<<j&l

24、t;<" no "<<setw(6);cin>>no;cout<<"please input student"<<j<<" score "for(i=0;i<N;i+)cin>>scorei;CStudent S1;cout<<"student"<<j<<" name"<<setw(6)<<"no"cout<<setw(1

25、5)vv"程序设计"<<setw(10)vv"信号处理"<<setw(10)vv"数据结构 "<<setw(10)<<"avg"<<endl;S1.setstudent(name,no,score);S1.showstudent();if(scorei<60)k+;cout«"不及格人数:"vvkvvendl;s “D:我的文若黨面Debug3.eHe"np le as e in putstudent1nane

26、 uangnp le as e in putstudent1no 02galease inpu.tstudent1score 56 7889student1 J nameno程序设计信号处理数据结构aug聪ng0256788?74.3333ple as & in putstudent21nane leiple as e in putstudent21no 0?Please inputstudent21scoi*e 67 8?78ktudent2 J nmeno程序设计信号处理数据结构avglei07&7897878please inputstudent3napte zliaap

27、lease inputstudent【3no 0Eple as e in putstudent3score 67 8968student3 nameno程序设计信号处理数据结构ausrhao05678966?46667不及格人数:3Press anv key to continue实验五派生与继承一单基派生5.1实验目的1.理解继承的概念;2 理解公有派生、私有派生和保护派生;3理解单基派生类中构造函数和析构函数的执行顺序。5.2实验内容1理解下面的程序并运行,然后回答后面的问题。#i nclude "iostream.h"class CBasepublic:CBase(i

28、nt a):a(a) protected:void print()cout<<"a="<<a<<endl; private:int a;class CDerive : public CBasepublic:void print()CBase:print(); cout<<"b="<<b<<endl; private:int b;void main()CDerive d;d. print();CBase b;b.print(); 问题一:以上程序有两个错误,试指出来,并改正之。 答:派

29、生类 CDerive 中没有定义 CDerive (),主函数中没有给 d, b 对象赋值。 #include "iostream.h"class CBasepublic:CBase(int a):a(a)void print()cout<<"a="<<a<<endl;private:int a;class CDerive : public CBasepublic:CDerive(int a,int c):CBase(a)b=c;void print()CBase:print(); cout<<"

30、b="<<b<<endl;private:int b;void main()CDerive d(1,3);d.print();CBase b(2);b.print();2理解下面的程序并运行,然后回答后面的问题。#include "iostream.h"class CBasepublic:CBase(int a):a(a)cout<<"base structure"<<endl;CBase()cout<<"base destructure"<<endl;

31、void print()cout<<"a="<<a<<endl;protected:int a;class CDerive : public CBasepublic:CDerive(int a, int b,int c):CBase(a),b(b),c(c)cout<<"derive structure"<<endl;CDerive()cout<<"derive destructure"<<endl;void print()CBase:print();

32、cout<<"b.a="<<b.a<<endl; cout<<"c="<<c<<endl; private:CBase b;int c;void main()CDerive d(1,2,3); d.print(); 问题一:以上程序的输出结果是什么,为什么? 答:程序错误,不能输出结果。“ cout<<"b.a="<<b.a<<endl; ”这个语句中调用了基类中的保护 参数 a。问题二:处语句执行完后,答: b.a=2。实验七

33、 多态性 函数与运算符重载7.1 实验目的1理解静态联编和动态联编的概念; 2掌握成员函数方式运算符重载; 3掌握友元函数方式运算符重载; 4掌握 +、- 、=运算符的重载。7.2 实验内容1理解下面的程序并运行,然后回答后面的问题。#include "iostream.h"class CComplexpublic:CComplex() real = 0; imag = 0;CComplex(int x,int y) real = x; imag = y;int real;int imag;CComplex operator + (CComplex obj1) CCompl

34、ex obj2(real + obj1.real, imag + obj1.imag); return obj2; ;void main()CComplex obj1(100,30);CComplex obj2(20, 30);CComplex obj;obj = obj1+obj2; cout << obj.real <<endl;cout << obj.imag << endl;问题一:处的运算符重载,为什么该函数的返回值要设计成CComplex类型?答:运算符重载函数的返回值与其操作类的类型相同。问题二:处的运算符重载函数调用就相当于&qu

35、ot;obj=operator+(obj1,obj2); ”,请问 CComplex类中的运算符重载函数为什么只有一个参数?答:因为另一个参数是隐含调用,是 CComplex 类的当前对象。它通过 this 指针隐含地进行传 递。2理解下面的程序并运行,然后回答后面的问题。#include "iostream.h" class CComplexpublic:CComplex()real = 0.0;imag = 0.0;CComplex(float x, float y)real = x;imag = y;CComplex operator + (CComplex &

36、;obj1, CComplex &obj2)CComplex obj3(obj1.real + obj2.real, obj1.imag + obj2.imag); return obj3;CComplex &operator+(CComplex &obj)obj.real += 1;obj.imag +=1;return obj;void print() cout<<real<<"+"<<imag<<"i"<<endl;private:float real;float

37、imag;CComplex &operator-(CComplex &x)x.real -= 1;x.imag -= 1;return x;void main()CComplex obj1(2.1,3.2);CComplex obj2(3.6,2.5);cout<<"obj1="obj1.print();cout<<"obj2="obj2.print();CComplex obj3 = obj1 + obj2;cout<<"befor+, obj3="obj3.print();+ob

38、j3;cout<<"after+, obj3="obj3.print();-obj3;cout<<"after-, obj3="obj3.print();CComplex obj4 = +obj3;cout<<"obj4="obj4.print(); 问题一:以上程序中的三个运算符重载都有错误,试改正并分析输出结果。 #include "iostream.h"class CComplexpublic:CComplex()real = 0.0;imag = 0.0;CComplex

39、(float x, float y)real = x;imag = y;CComplex operator + (CComplex &obj1)CComplex obj2(real + obj1.real, imag + obj1.imag); return obj2;friend CComplex operator+(CComplex &obj)obj.real += 1;obj.imag += 1;return obj;CComplex operator-();void print()cout << real << "+" <

40、;< imag << "i" << endl;private:float real;float imag;CComplex CComplex:operator-()real -= 1;imag -= 1;return (*this);void main()CComplex obj1(2.1, 3.2);CComplex obj2(3.6, 2.5);19cout << "obj1="obj1.print();cout << "obj2="obj2.print();CComplex obj3 = obj1 + obj2;cout << "befor+, obj3="obj3.print();+obj3;cout << "after+, obj3="obj3.print();-obj3;cout << "after-, obj3="obj3.print();CComplex obj4 = +obj3;cout << "obj4="CComplex obj4.print();结果:obj1=2.1+3.2;obj2=3.1+2.5;obj

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论