赠予燕大学弟学妹:
下面这些只是一种参考方法,并不是一定要这样写的。方法思想万卷不离其宗,只要掌握了知识点就都没问题的,下午答辩时候按照自己的理解回答老师的问题就可以了,不用紧张,只要知识点会无论怎样能表达出来就可以了。
PS:大一代码写的很烂,不喜勿喷 Thanks♪(・ω・)ノ
资源:实验报告.doc
给大家设置 0积分下载了,直接载下来参考就行啦: https://download.csdn.net/download/weixin_45525272/18736257
另外:其他带积分资源 没积分不知道咋下的 可以参考这一篇文章:
https://yangyongli.blog.csdn.net/article/details/116832457
C++面向对象程序设计实验报告
实验1 编程环境的熟悉及简单程序的编制
1.1 实验目的和要求 1.熟悉 VC++2010 编程环境,编制简单 C++程序并运行,熟悉 C++的编辑、 编译、 连接、运行、断点调试等过程。 2.掌握 C++数据类型,熟悉如何定义和使用常量和变量,以及对它们赋值的方法。 3.学会使用 C++的有关算术运算符及表达式,特别是自加(++)和自减(–)运算符 的使用。 4.分支和循环结构的使用 1.2 所占学时数 本实验安排 2 个实验课时。 1.3 实验任务
1.3.1 任务一
(1)题目名称 功能需求:运行时显示"Menu: A(dd) D(elete) S(ort) Q(uit), Select one:“提示 用户输入,A 表示增加,D 表示删除,S 表示排序,Q 表示退出,输入为 A、 D、S 时分别提示” 数据已经增加、删除、排序。"输入为 Q 时程序结束。 按照上述功能需求写两个程序,分别使用if分支语句和switch分支语句实现: 程序1要求:使用 if … else 语句进行判断,用 break、continue 控制程序流程。 程序2要求:使用 Switch 语句实现。
(2)构思过程(可用文字、流程图、UML图等方式表达) 在一个大的循环中可以无限次地输入、输出,将输入的内容去初始化一个新的字符,再对该字符进行对应操作: 程序1.用if进行分类,逐步套用 程序2.用case进行分类,逐步套用
(3)程序源码
程序1 :
#include <iostream>
using namespace std;
int main ( )
{
cout<< "提示用户输入,A 表示增加,D 表示删除,S 表示排序,Q 表示退出" << endl;
char C;
for ( ; ; )
{ cin>> C;
if ( C== 'A' )
{
cout<< "数据已经增加" << endl;
continue ;
}
else if ( C== 'D' )
{
cout<< "数据已经删除" << endl;
continue ;
}
else if ( C== 'S' )
{
cout<< "数据已经排序" << endl;
continue ;
}
else if ( C== 'Q' )
break ;
else
cout<< "请按提示输入!" << endl;
}
return 0 ;
}
程序2 :
#include <iostream>
using namespace std;
int main ( )
{
cout<< "提示用户输入,A 表示增加,D 表示删除,S 表示排序,Q 表示退出" << endl;
char C= ' ' ;
for ( ; ; )
{ cin>> C;
switch ( C)
{
case 'A' :
cout<< "数据已经增加" << endl;
break ;
case 'D' :
cout<< "数据已经删除" << endl;
break ;
case 'S' :
cout<< "数据已经排序" << endl;
break ;
case 'Q' :
break ;
default :
cout<< "请按恪提示输入" << endl;
}
}
return 0 ;
}
(4)运行结果(截图) 程序1:
(5)心得体会 在循环语句中,break和continue的作用不同:break表示是终止整个循环体,而continue表示是终止这一次循环,进入下一个循环。
1.3.1 任务二
(1)题目名称 找出2~10000之内的所有完全数。所谓完全数,即其各因子之和正好等于本身的数。如 6=1+2+3,28=1+2+4+7+14,所以6,28都是完全数。
(2)构思过程(可用文字、流程图、UML图等方式表达) 使函数在规定范围内判断该数是否是完全数:在循环中使该数对从2开始到它自身进行取余运算,将整除的因子相加(除自身外)求和。If语句进行判断,若是和与自身相同,则返回值为1;反之,返回值为0.主函数中,输出返回值为1的完全数。
(3)程序源码
#include <iostream>
#include <cmath>
using namespace std;
int yinzi ( int x)
{
int sum = 1 ;
if ( x >= 2 )
{
for ( int i = 2 ; i < x; i++ )
{
if ( x% i == 0 )
{
sum + = i;
}
else
continue ;
}
if ( sum == x)
return 1 ;
else
return 0 ;
}
}
int main ( )
{
for ( int i = 2 ; i <= 10000 ; i++ )
{
int s = yinzi ( i) ;
if ( s == 1 )
{
cout << i << ends;
}
else
continue ;
}
}
(4)运行结果(截图)
(5)心得体会 函数返回值可以借助计算机语言0和1简化操作
实验2 函数的应用
2.1 实验目的和要求 1.掌握函数声明、定义和调用的方法; 2.掌握函数递归调用的方法; 3.掌握重载函数的声明、定义和使用方法; 4.理解函数参数传递中传值与引用的区别 2.2 所占学时数 本实验安排 2 个实验课时。 2.3 实验任务
2.3.1 任务一
(1)题目名称 用递归方法编写函数 Fibonnacci(斐波那契)级数: Fn=Fn-1 + Fn-2; F1=F2=1; 并求出第 26 项的值。 (2)构思过程(可用文字、流程图、UML图等方式表达) 利用递归思想构建一个函数表示斐波那契数列,在主程序里引用函数体直接得出数列第16项并输出。
(3)程序源码
#include <iostream>
#include <cmath>
using namespace std;
int fbnq ( int n)
{
if ( n == 1 || n == 2 )
return 1 ;
else
return fbnq ( n - 1 ) + fbnq ( n - 2 ) ;
}
int main ( )
{
cout << fbnq ( 26 ) ;
}
(4)运行结果(截图)
(5)心得体会 函数的构建需要确定函数类型及形参的数据类型,引用时同样也注意实参的数据类型是否与形参相同,以及返回值类型。
2.3.2 任务二
(1)题目名称 设计一函数,判断一整数是否为素数。并完成下列程序设计: ①编写求素数的函数,并用这个函数求 3-200 之间的所有素数 ②在 4-200 之间,验证歌德巴赫猜想:任何一个充分大的偶数都可以表示为两个素数之和。输出 4=2+2 6=3+3 …… 200=3+197 注:素数又称质数,指在一个大于 1 的自然数中,除了 1 和此整数自身外,不能被其他自然数(不包括 0)整除的数
(2)构思过程(可用文字、流程图、UML图等方式表达) 先构造出一个判断素数的函数,在大循环中让该数依次去除以比它小的数,直至循环可以循环到该数前一个,输出。 验证哥德巴赫猜想时,任意偶数可以分成两素数的和,简化验证过程,我们可以直接验证每个偶数都可以分为3和另一个素数和(除4外)。
(3)程序源码
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int sushu ( int x)
{
if ( x > 1 )
{
for ( int i = 2 ; i <= x; i++ )
{
int r = x % i;
if ( r == 0 )
{
break ;
}
else
{
continue ;
}
if ( i = x - 1 )
{
return 1 ;
}
}
}
else
return 0 ;
}
string gdbh ( int y)
{
int i = 3 ;
if ( y == 4 )
{
return "2+2" ;
}
else
{
int j = y - 3 ;
int J = sushu ( j) ; ;
stringstream j0;
j0 << j;
string j1 = j0. str ( ) ;
if ( J = 1 )
{
return j1 + "+3" ;
}
else
return "错误" ;
}
}
int main ( )
{
for ( int i = 3 ; i <= 200 ; i++ )
{
int a = sushu ( i) ;
if ( a == 1 )
cout << i << ends;
}
cout << endl;
for ( int i = 4 ; i <= 200 ; i = i + 2 )
{
cout << i << "=" << gdbh ( i) << ends;
}
}
(4)运行结果(截图)
(5)心得体会 判断素数时可以借用break和continue来简化函数循环语句 预处理文件#include 中的stringstream和str()可以将int型转化为string型
2.3.3 任务三
(1)题目名称 ①比较值传递和引用传递的相同点和不同点。 ②调用被重载的函数时,通过什么来区分被调用的是哪一个函数? (2)答: ① 1、相同点:都是用来初始化对象的 2、不同点: 传值是单向的,形参的改变不会影响到实参; 传址是双向的,形参可以改变实参,数据不安全。 ② 重载函数名是相同的,但是函数的参数类型或者个数会不同,可以通过观察参数的类型和个数进行判断调用的函数。
实验3 类与对象、构造与析构函数
3.1 实验目的和要求 1.掌握类的定义和使用。 2.掌握类的定义和对象的声明。 3.掌握具有不同访问属性的成员的访问方式。 4.深入体会构造函数、复制构造函数、析构函数的执行机制。 5.使用的 VC++的 debug 调试功能观察程序流程,跟踪观察类的构造函数、析构函数、 成员函数的执行顺序。 3.2 所占学时数 本实验安排 4 个实验课时。 3.3 实验任务
3.3.1 任务一
(1)题目名称 设计一个用于人事管理的 People(人员)类。考虑到通用性,这里只抽象出所有类型人员都具有的属性:number(编号)、sex(性别)、birthday(出生日期)、id(身份 证号)等。 其中"出生日期"定义为一个"日期"类内嵌子对象。 用成员函数实现对人员信息 的录入和显示。 要求包括:构造函数和析构函数、拷贝构造函数、内联成员函数、带缺省形参值的成员函数 、聚集。
(2)构思过程(可用文字、流程图、UML图等方式表达) 定义Date类,利用类的构造函数声明输入和输出的两个操作函数对内嵌子对象birthday进行操作。 定义People类,初始化基础成员函数,利用公有成员函数进行People对象的输入。 在主函数中声明People类的对象进行输入,利用People类的复制构造函数进行输出。
(3)程序源码
#include <iostream>
#include <string>
using namespace std;
class Date
{
public :
Date ( int x = 0 , int y = 0 , int z = 0 )
{
year = x;
month = y;
day = z;
} ;
void setdate ( ) ;
void showdate ( ) ;
private :
int year, month, day;
} ;
void Date:: setdate ( )
{
int a, b, c;
cout << "输入日期" << ends;
cin >> a >> b >> c;
year = a;
month = b;
day = c;
}
void Date:: showdate ( )
{
cout << year << "年" << month << "月" << day << "日" << endl;
}
class People
{
public :
People ( ) { } ;
People ( const People& p) ;
~ People ( ) { }
void setpeople ( ) ;
void showpeople ( ) ;
private :
int number;
long idnumber;
string sex;
Date birthday;
} ;
People:: People ( const People& p)
{
number = p. number;
birthday = p. birthday;
sex = p. sex;
idnumber = p. idnumber;
}
void People:: setpeople ( )
{
Date a;
int b;
long c;
string d;
a. setdate ( ) ;
cout << "号码:" ;
cin >> b;
cout << "id:" ;
cin >> c;
cout << "性别:" ;
cin >> d;
cout << endl;
birthday = a;
number = b;
idnumber = c;
sex = d;
}
void People:: showpeople ( )
{
cout << "number:" << ends << number << endl;
cout << "idnumber:" << ends << idnumber << endl;
cout << "sex:" << ends << sex << endl;
cout << "birthday:" << ends;
birthday. showdate ( ) ;
}
int main ( )
{
People p1;
p1. setpeople ( ) ;
People p2 ( p1) ;
p1. showpeople ( ) ;
return 0 ;
}
(4)运行结果(截图)
(5)心得体会 利用拷贝构造函数可以更方便地实现从输入转到输出的过程 一个类的对象可以在另一个类中作为成员函数 字符串的使用一定要在代码前加入预处理#include
实验4 数据共享与保护
4.1 实验目的和要求 1.观察程序运行中变量的作用域、生存期。 2.学习类的静态成员的使用。 3.理解类的友元函数、友元类。 4. 学习多文件结构在 C++程序中的使用。 4.2 所占学时数 本实验安排 4 个实验课时。 4.3 实验任务 4.3 任务一
(1)题目名称 定义一个 Girl 类和一个 Boy 类,这两个类中都有表示姓名、年龄的私有成员变量, 都要定义构造函数、析构函数、输出成员变量信息的公有成员函数。 删除两个类中的函数 visitgirl(girl &),visitboy(boy &) ,定义一个顶层函数 visitboygirl(boy &,girl &),作为以上两个类的友元,通过调用该函数输出男孩和女孩的信息。
(2)构思过程(可用文字、流程图、UML图等方式表达) 定义两个类,在其中分别定义出它的构造函数和析构函数并声明出最终的顶层友元函数。利用构造函数把私有数据初始化。在主函数中声明类的对象,利用顶层函数进行输出。
(3)程序源码
#include <iostream>
#include <string>
using namespace std;
class Girl ;
class Boy
{
private :
string name;
int age;
public :
Boy ( string theName, int theAge) ;
friend void visitboygirl ( Boy & b, Girl & g) ;
~ Boy ( ) { }
} ;
Boy:: Boy ( string theName, int theAge)
{
name= theName;
age= theAge;
}
class Girl
{
private :
string name;
int age;
public :
Girl ( string theName, int theAge) ;
friend void visitboygirl ( Boy & b, Girl & g) ;
~ Girl ( ) { }
} ;
Girl:: Girl ( string theName, int theAge)
{
name= theName;
age= theAge;
}
int main ( )
{
Girl g ( "Mary" , 18 ) ;
Boy b ( "Jack" , 19 ) ;
visitboygirl ( b, g) ;
}
void visitboygirl ( Boy & b, Girl & g)
{
cout<< "Girl's name:" << g. name<< endl;
cout<< "Girl's age:" << g. age<< endl;
cout<< "Boy's name:" << b. name<< endl;
cout<< "Boy's age:" << b. age<< endl;
}
(4)运行结果(截图)
(5)心得体会 友元类不能传递且友元关系是单向的; 若A为B的友元类,则A中所有函数成员都可以访问B的私有和保护成员;
实验5 数组、指针与字符串
5.1 实验目的和要求 1.学习使用数组。 2.掌握指针的使用方法,体会运算符&、*的不同作用。 3. 学习字符串数据的组织和处理。 4. 练习通过动态分配内存实现动态数组,并体会指针在其中的作用。 5.分别使用字符数组和标准 C++库练习处理字符串的方法。 5.2 所占学时数 本实验安排 4 个实验课时。 5.3 实验任务
5.3 任务一
(1)题目名称 用类来实现矩阵,定义一个矩阵的类,属性包括: 矩阵大小,用 lines, rows(行、列来表示); 存贮矩阵的数组指针,根据矩阵大小动态申请(new)。 矩阵类的方法包括: 构造函数:参数是矩阵大小,需要动态申请存贮矩阵的数组; 析构函数:需要释放矩阵的数组指针; 拷贝构造函数:需要申请和复制数组(深复制); 输入函数:可以从 cin 中输入矩阵元素; 输出函数:将矩阵格式化输出到 cout; 矩阵相加函数:实现两个矩阵相加的功能,结果保存在另一个矩阵里,但必须矩阵 大小相同; 矩阵相减的函数:实现两个矩阵相减的功能,结果保存在另一个矩阵里,但必须矩 阵大小相同。 主函数功能: 定义三个矩阵:A1、A2、A3; 初始化 A1、A2; 计算并输出 A3 = A1+A2,A3=A1+A2; 用 new 动态创建三个矩阵类的对象:pA1、pA1、pA3; 初始化 pA1、pA2; 计算并输出 pA3=pA1+pA2,pA3=pA1+pA2; 释放 pA1、pA1、pA3。
(2)构思过程(可用文字、流程图、UML图等方式表达) 利用复制构造函数实现矩阵的输入,避免使用数组出现混乱 学习指针的用法以及注意事项 (3)程序源码
#include <iostream>
using namespace std;
class Matrix
{
public :
Matrix ( ) ;
Matrix ( int line, int row) ;
Matrix ( const Matrix & m) ;
void set ( ) ;
void show ( ) ;
~ Matrix ( ) ;
friend void plus ( const Matrix & a, const Matrix & b, Matrix & m) ;
friend void sub ( const Matrix & a, const Matrix & b, Matrix & m) ;
private :
int lines; int rows; int * * p;
} ;
void Matrix:: set ( )
{
for ( int i = 0 ; i < lines; i++ )
{
for ( int j = 0 ; j < rows; j++ )
{
cout << "请输入第”<<i+1<<" 行, 第” << j + 1 << "列元素:" << ends;
cin >> p[ i] [ j] ;
cout << endl;
}
}
}
Matrix:: Matrix ( int line, int row) : lines ( line) , rows ( row)
{
lines = line; rows = row;
p = new int * [ lines] ;
for ( int i = 0 ; i < lines; i++ )
{
p[ i] = new int [ rows] ;
}
}
Matrix:: Matrix ( const Matrix & m)
{
lines = m. lines; rows = m. rows;
p = new int * [ rows] ;
for ( int i = 0 ; i < lines; i++ )
{
for ( int j = 0 ; j < rows; j++ )
{
p[ i] [ j] = m. p[ i] [ j] ;
}
}
}
Matrix:: ~ Matrix ( )
{
for ( int i = 0 ; i < lines; i++ )
{
delete p[ i] ;
}
delete [ ] p;
}
void Matrix:: show ( )
{
for ( int i = 0 ; i < lines; i++ )
{
for ( int j = 0 ; j < rows; j++ )
{
cout << p[ i] [ j] << " " ;
}
cout << endl;
}
}
void plus ( const Matrix & a, const Matrix & b, Matrix & m)
{
for ( int i = 0 ; i < a. lines; i++ )
{
for ( int j = 0 ; j < a. rows; j++ )
{
m. p[ i] [ j] = a. p[ i] [ j] + b. p[ i] [ j] ;
}
}
}
void sub ( const Matrix & a, const Matrix & b, Matrix & m)
{
for ( int i = 0 ; i < a. lines; i++ )
{
for ( int j = 0 ; j < a. rows; j++ )
{
m. p[ i] [ j] = a. p[ i] [ j] - b. p[ i] [ j] ;
}
}
}
int main ( )
{
Matrix A1 ( 2 , 3 ) , A2 ( 2 , 3 ) , A3 ( 2 , 3 ) ;
A1. set ( ) ;
A2. set ( ) ;
plus ( A1, A2, A3) ;
A3. show ( ) ;
Matrix * pA1 = new Matrix ( 2 , 3 ) ;
Matrix * pA2 = new Matrix ( 2 , 3 ) ;
Matrix * pA3 = new Matrix ( 2 , 3 ) ;
pA1- > set ( ) ;
pA2- > set ( ) ;
plus ( * pA1, * pA2, * pA3) ;
pA1- > show ( ) ;
pA2- > show ( ) ;
pA3- > show ( ) ;
delete pA1;
delete pA2;
delete pA3;
return 0 ;
}
(4)运行结果(截图)
(5)心得体会 复制函数可以大大提高代码的利用率
实验6 类的继承与派生
6.1 实验目的和要求 1、从深层次上理解继承与派生的关系 2、掌握不同继承方式下,从派生类/对象内部和外部对基类成员的访问控制权限。 3、掌握单继承和多继承的使用方法,尤其是派生类构造函数的声明方式。 4、掌握继承与派生下构造函数与析构函数的调用顺序。 5、理解“类型兼容”原则 。 6、学习利用虚基类解决二义性问题。 6.2 所占学时数 本实验安排 4 个实验课时。 6.3 实验任务
6.3.1 任务一
(1)题目名称 编写 C++程序,以完成以下功能(具体的数据成员、函数成员,请自主定义): (1)声明一个基类 Shape(形状),其中包含一个方法来计算面积; (2)从 Shape 派生两个类:矩形类(Rectangle)和圆形类(Circle); (3)从 Rectangle 类派生正方形类 Square; (4)分别实现派生类构造函数、析构函数及其它功能的成员函数; (5)创建各派生类的对象,观察构造函数、析构函数的调用次序; (6)计算不同对象的面积。
2)构思过程(可用文字、流程图、UML图等方式表达) 声明基类,要求其中的函数可以在派生类中同样适用(公有派生) 声明派生类,确定公有成员和私有成员,引用基类中的面积area函数,并具体定义area函数中的计算方式 在主函数中依次输入边长(半径)的数值,返回函数并输出
(3)程序源码
#include <iostream>
#include <cmath>
using namespace std;
class Shape
{
public :
float area ( ) ;
} ;
class Circle : public Shape
{
private :
float radius;
public :
Circle ( float r) { radius= r; } ;
float area ( )
{
return radius* radius* 3.14 ;
}
~ Circle ( ) { }
} ;
class Rectangle : public Shape
{
private :
float l, w;
public :
Rectangle ( float a, float b)
{
l= a;
w= b;
} ;
float area ( )
{
return l* w;
}
~ Rectangle ( ) { }
} ;
class Square : public Rectangle
{
private :
float x;
public :
Square ( float c) : Rectangle ( c, c) { x= c; }
float area ( )
{
return x* x;
}
~ Square ( ) { }
} ;
int main ( )
{
float a, b;
cout<< "请输入矩形长和宽:" ;
cin>> a>> b;
Rectangle R ( a, b) ;
cout<< "矩形面积为:" << R. area ( ) << endl;
float c;
cout<< "请输入圆半径:" ;
cin>> c;
Circle C ( c) ;
cout<< "圆的面积为:" << C. area ( ) << endl;
float d;
cout<< "请输入正方形的边长:" ;
cin>> d;
Square S ( d) ;
cout<< "正方形的面积为:" << S. area ( ) << endl;
}
(4)运行结果(截图)
(5)心得体会 在代码运行中出现了无输出的情况,检查下发现主函数的代码顺序错误,在C++编程中要注意代码运行顺序 基类派生类的派生类在继承上一个派生类数据时不可以直接运用
实验7 多态性
7.1 实验目的和要求 1、 掌握将运算符重载为成员函数与非成员函数的区别。 2、 掌握静态编联与动态联编的概念、区别及实现方式。 2、 掌握利用虚函数实现动态多态的方法。 3、 掌握利用纯虚函数与抽象类实现动态多态的方法。
7.2 所占学时数 本实验安排 2 个实验课时。 7.3 实验任务
7.3 任务一
(1)题目名称 定义一个基类为哺乳动物类 mammal,其中有数据成员年龄、重量、品种,有成员函数 move()、speak()、eat()等,以此表示动物的行为。由这个基类派生出狗、猫、马、猪等哺乳动物,它们都有各自的行为。编程分别使各个动物表现出不同的行为。
(2)构思过程(可用文字、流程图、UML图等方式表达) 1、为实现动态联编,首先建立 Mammal 抽象类,以此抽象类作为基类,派生 dog、cat、 horse、pig 类。其中 Mammal 类数据员有(姓名)name 、(年龄)age、(重量)weight。成员函数 move()、eat()、speak(),定义为纯虚函数:另一个成员函数 display(),声明为虚函数。 2、建立各个派生类 dog、cat、horse、pig。然后建立构造函数为其初始化。再定义函数 move()、speak()、eat()等。 3、main() 函数中建立指向 Mammal 的指针数组,并为各派生类初始化。把指针数组分别指向各个派生类。设计一个循环来显示派生类对象的信息。
(3)程序源码
#include <iostream>
#include <string>
using namespace std;
class Mammal
{
public :
virtual void move ( ) = 0 ;
virtual void speak ( ) = 0 ;
virtual void eat ( ) = 0 ;
virtual void display ( ) { } ;
private :
float age;
double weight;
string name ;
} ;
class Dog : virtual public Mammal
{
public :
Dog ( int a, double w, string n) ;
void move ( ) { cout<< name<< "爬行" << ends; }
void speak ( ) { cout<< name<< "汪" << ends; }
void eat ( ) { cout<< name<< "骨头? << ends; }
private :
int age;
double weight;
string name ;
} ;
Dog:: Dog ( int a, double w, string n) : age ( a) , weight ( w) , name ( n)
{
cout<< "种类:狗·" << ends;
cout<< "姓名:" << name<< ends;
cout<< "年龄:" << age<< ends;
cout<< "重量:" << weight<< "kg" << endl;
}
class Cat : virtual public Mammal
{
public :
Cat ( int a, double q, string n) ;
void move ( ) { cout<< name<< "爬行" << ends; }
void speak ( ) { cout<< name<< "喵" << ends; }
void eat ( ) { cout<< name<< "鱼" << ends; }
private :
int age;
double weight;
string name ;
} ;
Cat:: Cat ( int a, double w, string n) : age ( a) , weight ( w) , name ( n)
{
cout<< "种类:猫¨" << ends;
cout<< "姓名:" << name<< ends;
cout<< "年龄:" << age<< ends;
cout<< "重量:" << weight<< "kg" << endl;
}
class Horse : virtual public Mammal
{
public :
Horse ( int a, double w, string n) ;
void move ( ) { cout<< name<< "爬行" << ends; }
void speak ( ) { cout<< name<< "吼" << ends; }
void eat ( ) { cout<< name<< "草" << ends; }
private :
int age; //年龄
double weight; //质量
string name ; //种类
} ;
Horse:: Horse ( int a, double w, string n) : age ( a) , weight ( w) , name ( n)
{
cout<< "种类:马" << ends;
cout<< "姓名:" << name<< ends;
cout<< "年龄:" << age<< ends;
cout<< "重量:" << weight<< "kg" << endl;
}
class Pig : virtual public Mammal
{
public :
Pig ( int a, double w, string n) ;
void move ( ) { cout<< name<< "爬行" << ends; }
void speak ( ) { cout<< name<< "哼" << ends; }
void eat ( ) { cout<< name<< "杂食" << ends; }
private :
int age; //年龄
double weight; //质量
string name ; //种类
} ;
Pig:: Pig ( int a, double w, string n) : age ( a) , weight ( w) , name ( n)
{
cout<< "种类:阰猪í" << ends;
cout<< "姓名:" << name<< ends;
cout<< "年龄:" << age<< ends;
cout<< "重量:" << weight<< "kg" << endl;
}
void main ( )
{
Mammal * m[ 4 ] ;
Dog d ( 0.2 , 30 , "小狗" ) ;
Cat c ( 1 , 10 , "小猫" ) ;
Horse h ( 5 , 60 , "小马" ) ;
Pig p ( 1 , 200 , "小猪" ) ;
m[ 0 ] = & d;
m[ 1 ] = & c;
m[ 2 ] = & h;
m[ 3 ] = & p;
for ( int i= 0 ; i< 4 ; i++ )
{
m[ i] - > move ( ) ;
m[ i] - > speak ( ) ;
m[ i] - > eat ( ) ;
}
}
(4)运行结果(截图)
(5)心得体会 抽象类不能实例化,只能通过指针和引用访问派生类的对象(多态性)
实验8 交通灯设计
8.1 实验目的和要求 1.熟悉Arduino编程环境,编制简单C++程序并运行,熟悉 C++的编辑、 编译、连接、运行、断点调试等过程。 2.了解交通灯设计实验的电子原理图,熟悉电路板布局图,熟悉烧制程序到电路板。 3. 掌握交通灯程序的设计和运行原理,并能够根据自己的能力做相应的扩展 8.2 所占学时数 本实验安排 2 个实验课时。 8.3 实验任务
8.3 任务一
(1)题目名称 在Arduino上烤制下面程序,观察电路板效果。 ①修改代码改变小灯延迟时间,观察效果。 ②修改代码改变小灯开关逻辑,生成自定义的效果
(2)构思过程(可用文字、流程图、UML图等方式表达) 参考已有PPT和实验视频,了解熟悉Arduino的使用和操作,根据线路图安装好器材(选择合适的电阻),在软件上输入题目给出的代码,连接好电脑和线路板的接口进行实验操作
成功后修改代码中的部分数据进行实验,了解每个语句的作用 修改语句的逻辑顺序,观察实验结果 (3)程序源码
int redled = 10 ; //定义数字10 接口
int yellowled = 7 ; //定义数字7 接口
int greenled = 4 ; //定义数字4 接口
void setup ( )
{
pinMode ( redled, OUTPUT) ; //定义红色小灯接口为输出接口
pinMode ( yellowled, OUTPUT) ; //定义黄色小灯接口为输出接口
pinMode ( greenled, OUTPUT) ; //定义绿色小灯接口为输出接口
}
void loop ( )
{
digitalWrite ( redled, HIGH) ; //点亮红色小灯
delay ( 1000 ) ; //延时1 秒
digitalWrite ( redled, LOW) ; //熄灭红色小灯
digitalWrite ( yellowled, HIGH) ; //点亮黄色小灯
delay ( 200 ) ; //延时0.2 秒
digitalWrite ( yellowled, LOW) ; //熄灭黄色小灯
digitalWrite ( greenled, HIGH) ; //点亮绿色小灯
delay ( 1000 ) ; //延时1 秒
digitalWrite ( greenled, LOW) ; //熄灭绿色小灯
}
(4)运行结果(截图)
(5)心得体会 根据线路图选择合适的电阻 Arduino中自带delay函数 代码中的接口位置要与线路板上的连接接口相同
实验9 数组、指针与字符串
9.1 实验目的和要求 1.熟悉Arduino编程环境,编制简单C++程序并运行,熟悉 C++的编辑、 编译、连接、运行、断点调试等过程。 2.了解数码管设计实验的电子原理图,熟悉电路板布局图,熟悉烧制程序到电路板。 3. 掌握数码管程序的设计和运行原理,并能够根据自己的能力做相应的扩展 4.分支和循环结构的使用 9.2 所占学时数 本实验安排 2 个实验课时。 9.3 实验任务 9.3 任务一 (1)题目名称 在Arduino上烤制下面程序,观察电路板效果。 ①修改代码改变数码管延迟时间,观察效果 ②修改代码改变数码管逻辑,生成自定义的效果。(入生成“ABCDEF”的效果)
(2)构思过程(可用文字、流程图、UML图等方式表达) 依照PPT和实验视频,了解数码管的内部结构
按照电路图连接好器材
根据已有程序代码运行操作观察实验现象,同时进行修改来了解语句作用。
(3)程序源码
int a = 7 ; //定义数字接口7 连接a 段数码管
int b = 6 ; // 定义数字接口6 连接b 段数码管
int c = 5 ; // 定义数字接口5 连接c 段数码管
int d = 11 ; // 定义数字接口11 连接d 段数码管
int e = 10 ; // 定义数字接口10 连接e 段数码管
int f = 8 ; // 定义数字接口8 连接f 段数码管
int g = 9 ; // 定义数字接口9 连接g 段数码管
int dp = 4 ; // 定义数字接口4 连接dp 段数码管
void digital_1 ( void ) //显示数字1
{
unsigned char j;
digitalWrite ( c, HIGH) ; //给数字接口5 引脚高电平,点亮c 段
digitalWrite ( b, HIGH) ; //点亮b 段
for ( j = 7 ; j <= 11 ; j++ ) //熄灭其余段
digitalWrite ( j, LOW) ;
digitalWrite ( dp, LOW) ; //熄灭小数点DP 段
}
void digital_2 ( void ) //显示数字2
{
unsigned char j;
digitalWrite ( b, HIGH) ;
digitalWrite ( a, HIGH) ;
for ( j = 9 ; j <= 11 ; j++ )
digitalWrite ( j, HIGH) ;
digitalWrite ( dp, LOW) ;
digitalWrite ( c, LOW) ;
digitalWrite ( f, LOW) ;
}
void digital_3 ( void ) //显示数字3
{
unsigned char j;
digitalWrite ( g, HIGH) ;
digitalWrite ( d, HIGH) ;
for ( j = 5 ; j <= 7 ; j++ )
digitalWrite ( j, HIGH) ;
digitalWrite ( dp, LOW) ;
digitalWrite ( f, LOW) ;
digitalWrite ( e, LOW) ;
}
void digital_4 ( void ) //显示数字4
{
digitalWrite ( c, HIGH) ;
digitalWrite ( b, HIGH) ;
digitalWrite ( f, HIGH) ;
digitalWrite ( g, HIGH) ;
digitalWrite ( dp, LOW) ;
digitalWrite ( a, LOW) ;
digitalWrite ( e, LOW) ;
digitalWrite ( d, LOW) ;
}
void digital_5 ( void ) //显示数字5
{
unsigned char j;
for ( j = 7 ; j <= 9 ; j++ )
digitalWrite ( j, HIGH) ;
digitalWrite ( c, HIGH) ;
digitalWrite ( d, HIGH) ;
digitalWrite ( dp, LOW) ;
digitalWrite ( b, LOW) ;
digitalWrite ( e, LOW) ;
}
void digital_6 ( void ) //显示数字6
{
unsigned char j;
for ( j = 7 ; j <= 11 ; j++ )
digitalWrite ( j, HIGH) ;
digitalWrite ( c, HIGH) ;
digitalWrite ( dp, LOW) ;
digitalWrite ( b, LOW) ;
}
void digital_7 ( void ) //显示数字7
{
unsigned char j;
for ( j = 5 ; j <= 7 ; j++ )
digitalWrite ( j, HIGH) ;
digitalWrite ( dp, LOW) ;
for ( j = 8 ; j <= 11 ; j++ )
digitalWrite ( j, LOW) ;
}
void digital_8 ( void ) //显示数字8
{
unsigned char j;
for ( j = 5 ; j <= 11 ; j++ )
digitalWrite ( j, HIGH) ;
digitalWrite ( dp, LOW) ;
}
void setup ( )
{
int i; //定义变量
for ( i = 4 ; i <= 11 ; i++ )
pinMode ( i, OUTPUT) ; //设置4~11 引脚为输出模式
}
void loop ( )
{
while ( 1 )
{
digital_1 ( ) ; //显示数字1
delay ( 2000 ) ; //延时2s
digital_2 ( ) ; //显示数字2
delay ( 1000 ) ; //延时1s
digital_3 ( ) ; //显示数字3
delay ( 1000 ) ; //延时1s
digital_4 ( ) ; //显示数字4
delay ( 1000 ) ; //延时1s
digital_5 ( ) ; //显示数字5
delay ( 1000 ) ; //延时1s
digital_6 ( ) ; //显示数字6
delay ( 1000 ) ; //延时1s
digital_7 ( ) ; //显示数字7
delay ( 1000 ) ; //延时1s
digital_8 ( ) ; //显示数字8
delay ( 1000 ) ; //延时1s
}
}
(4)运行结果(截图)
(5)心得体会 数码管使用时跟发光二极管一样,要连接限流电阻,防止电流 在代码操作控制数码管显示数字时,要求对应的二极管发光即可