面向对象程序设计(C)实验指导书.pdf

上传人:tbuqq 文档编号:4630598 上传时间:2019-11-22 格式:PDF 页数:22 大小:321.42KB
返回 下载 相关 举报
面向对象程序设计(C)实验指导书.pdf_第1页
第1页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《面向对象程序设计(C)实验指导书.pdf》由会员分享,可在线阅读,更多相关《面向对象程序设计(C)实验指导书.pdf(22页珍藏版)》请在三一文库上搜索。

1、1 / 22 面向对象程序设计( C+)课程实验指导书 安阳工学院计算机科学与信息工程学院 软件工程教研室 2018.9 2 / 22 编号: 课程总学时: 64 实验学时: 32 课程总学分: 3.5 实验学分: 先修课程: C语言程序设计 适用专业:计算机科学与技术,网络工程,软件工程 一、 本课程实验的主要目的与任务 面向对象程序设计(C+)是计算机专业学生的一门专业基础课。C+是一种高效而 又实用的程序设计语言,它既可以进行过程化程序设计,也可以进行面向对象程序设计, 因此成为了编程人员最广泛使用的工具。主要任务是介绍C+语言中的数据类型,运算, 语句结构及其程序设计的基本方法。使学生

2、掌握一门高级程序设计语言,了解面向对象程 序设计的基本概念与方法,进而学会利用C+语言学会解决一般应用问题,从而掌握面向 对象程序设计的基本知识和基本技能。并为后续的专业课程奠定程序设计基础。 实验 1 C+ 基础 一、实验目的 1.加强学生掌握C+的基本知识点; 2.加强学生掌握I/O 流; 3 加强学生进一步理解函数的用法; 4 理解引用的概念及应用。 三、实验内容 1.用函数返回值实现统计A 类学生和B 类学生个数,平均分大于等于80 的为 A 类,其余 为 B 类。 四. 实验指导 1.参考程序: #include int main() cout #include / 要用到格式控制符

3、 void main() 3 / 22 double amount = 22.0/7 。 cout #include int main() int number=1001 。 cout #include int main() cout #include 4 / 22 void main() cout void main() const float PI=3.1415926f 。 float r 。 float z,s。 coutr 。 z=2*PI*r 。 s=PI*r*r 。 cout using namespace std。 int array64=60,80,90,75, 75,85,6

4、5,77, 80,88,90,98, 89,100,78,81, 62,68,69,75, 85,85,77,91 。 int& level(int grade, int size, int& tA, int& tB)。 int main() int typeA=0,typeB=0 。 int student=6。 int gradesize=4。 for(int i=0 。 i=80) return tA 。 /type A student else return tB 。 /type B student 运行结果: 实验 2 类和对象 1、实验目的: 掌握类的定义,根据具体需求设计类; 深

5、入理解 C+中类的封装性; 会根据类创建各种对象; 掌握对象的各种成员的使用方法。 2、实验内容 定义一个满足如下要求的Date 类。 (1)用下面的格式输出日期:日/月/年; (2)可运行在日前上加一天操作; (3)设置日期。 参考代码: #include class Date 6 / 22 public: void Display() 。 void AddOneDay() 。 void SetDay(int y,int m,int d) 。 protected: bool Legal(int y, int m, int d) 。 bool IsLeapYear(int y) 。 int y

6、ear。 int month 。 int day。 。 void Date:Display() cout 9999|y12) return false。 int dayLimit=31 。 switch(m) case 4: case 6: case 9: case 11: dayLimit- 。 if(m=2) dayLimit = IsLeapYear(y) ? 29 : 28。 return (ddayLimit)? false : true 。 7 / 22 bool Date:IsLeapYear(int y) return !(y%4)&(y%100)|!(y%400)。 int

7、main() Date d。 d.SetDay(2018,2,28)。 d.Display() 。 d.AddOneDay() 。 d.Display() 。 system(“PAUSE“) 。 运行结果 : 实验 3 继承与派生 1、实验目的: 理解继承的概念,学习如何使用基类成员,了解基类成员在派生类中的访问控制; 理解类的派生对代码复用的作用。 2、实验内容: 设计一个人员类person 和一个日期类date,由人员类派生出学生类Student 和教师类 professor,学生类和教师类的数据成员birthday 为日期类。 参考代码: #include #include using

8、namespace std。 class date public: date() coutyearmonthday 。 void display() 8 / 22 coutnamestr。 name = new charstrlen(namestr)+1 。 strcpy(name,namestr)。 class student:public person private: int ID 。 int score。 date birthday。 public: student() coutID 。 coutscore。 void display() coutNo 。 coutmajor 。 vo

9、id display() cout #include using namespace std。 class shape public: virtual double area()=0 。 virtual void display() = 0 。 shape() 。 class rectangle:public shape public: rectangle(double a =1,double b=1) x = a。 y = b。 double area() return x*y 。 11 / 22 void display() coutdisplay() 。 p = new triangle

10、(3,4,5) 。 p-display() 。 p = new circles(2) 。 p-display() 。 system(“PAUSE“) 。 return 0。 运行结果: 实验 5 运算符重载 1、实验目的: 掌握运算符重载的概念及使用方法,掌握特殊运算符的重载要求和方法。 2、实验内容: 定义整数集合类intSet,实现如下功能: 定义类的构造函数,并根据需要可以定义多个构造函数。 (1)Clear 函数:清空整数集合 (2)IsEmpty(): 整数集合是否空集; (3)IsMemberOf(): 判断某个整数是否在整数集合内 (4)Operator+:增加一个整数到整数集合

11、中 (5)Operator-:从整数集合中删除一个整数元素; (6)Operator=: 判断两个整数集合是否相等; (7)Operator*: 求两个整数结合的交集; 13 / 22 (8)Operator+:求两个整数集合的交集; (9)Operator=:整数集合的对象之间的赋值; (10)Operator using namespace std。 class intSet public: intSet()。 intSet(int a,int size) 。 void Clear() 。 bool IsEmpty() 。 bool IsMemberOf(int a) 。 bool ope

12、rator+(int a) 。 bool operator-(int a) 。 bool operator=(intSet & set) 。 intSet operator+(intSet &set) 。 intSet operator *(intSet &set) 。 void operator = (intSet &set) 。 friend ostream &operator=100) ElementNum = 99 。 else ElementNum = size -1 。 for(int i = 0 。i #include using namespace std。 class Stu

13、dent private: long No 。 char *Name。 int Score。 public: Student(long stu_no=0,char*stu_name=NULL,int stu_score=0)。 long GetNo() 。 char *GetName() 。 int GetScore() 。 void ShowStudent() 。 。 Student:Student(long stu_no,char *stu_name,int stu_score) No= stu_no。 Name = stu_name。 Score = stu_score。 void St

14、udent:ShowStudent() coutk 。 pos = (k-1)*sizeof(Student) 。 infile.seekg(pos) 。 infile.read(char*)&stui,sizeof(Student)。 cout,用于对T 类型的数组停止求一切元素的和、查找指定 的元素能否存在,假如存在,则返回其所在数组元素的下标值,否则返回-1 参考代码: #include #include using namespace std。 template class CArray private: T *m_pArray 。/成员变量为动态数组的起始指针 int m_nArra

15、ySize 。 public: CArray(int size = 1) 。/结构函数 CArray() 。/析构函数 T Sum()。/对数组停止求和 int Search(T x) 。 /查找数组索引下标 21 / 22 T &operator (int index)。/重载 符号 。 template CArray:CArray(int size) m_pArray = new Tsize 。 /动态申请数组 m_nArraySize = size 。 /数组的大小 memset(m_pArray, 0, sizeof(T)*size) 。/将数组空间清空 template CArray

16、:CArray() delete m_pArray 。 /释放动态分配的空间 m_pArray = NULL 。 template T CArray:Sum() T tSum = 0。 for(int i=0 。 i int CArray:Search(T x) for(int i=0 。 i T &CArray:operator (int index) if(indexm_nArraySize-1) cout arr(6) 。 for(int i=0 。 i6。 i+) arri = i 。 /给数组停止初始化 int sum = arr.Sum() 。 /数组求和 int index = arr.Search(3) 。/找寻 3 在数组中的下标。 cout“ 数组之和sum = “sumendl 。 cout“ 数字 3 在数组中的下标index 是: “indexendl 。 return 0。 运行结果:

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1