MOOS-ivp 实验二 C++编程练习(2)

MOOS-ivp 实验二 C++编程练习(2)


二、编程结构

CSDN突然格式出现错误,导致之前写的内容成为乱码。这里也比较简单基础,具体内容可参考网页:
C++

1.if 和 else结构

2.while-loop结构

3.for-loop结构

三、简单类

类的使用使得C++成为了面对对象编程的语言。详细内容可以参考网页C++类与对象

1.类的简单示例

1.创建一个文件,起名为 simple_rect.cpp
2.输入以下代码

1 // Code example from: http://www.cplusplus.com/doc/tutorial/classes/
2 // classes example
3 #include <iostream>
4 using namespace std;
5
6 class CRectangle {
    
    
7  int x, y;
8  public:
9  void set_values (int,int);
10  int area () {
    
    return (x*y);}
11 };
12
13 void CRectangle::set_values (int a, int b) {
    
    
14  x = a;
15  y = b;
16 }
17
18 int main () {
    
    
19  CRectangle rect;
20  rect.set_values (3,4);
21  cout << "area: " << rect.area() << endl;
22  return 0;
23 }

3.修改要求,需要增加一个表示颜色的成员变量,增加一个输出颜色的成员函数,给之前set_value添加修改颜色的参数。代码如下:

using namespace std;
class CRectangle{
    
    
        int x,y;
        string color;
    public:
        void set_values(int ,int ,string );
        int area(){
    
    return (x*y);}
        void print();
};
void CRectangle::set_values(int a,int b,string colors){
    
    
    x=a;
    y=b;
    color=colors;
}
void CRectangle::print(){
    
    
cout << "color:"<< color<<endl;
}
int main()
{
    
    
    CRectangle rect;
    rect.set_values(3,4,"Yellow");
    rect.print();
    cout << "area:"<<rect.area()<<endl;

2.在不同文件中构建程序

C++的一个好处就是一旦类与对象被定义,那么其就可以在多个程序文件中被使用。这个实验的目的在于将类的定义和类的实现分为不同的文件,使得该类的对象可以在多个程序中进行使用。
1.将以下代码编辑为三个文件:

1 // distributed_rect_main.cpp
2
3 #include <iostream>
4 #include "CRectangle.h"
5 using namespace std;
6
7 int main () {
    
    
8  CRectangle rect;
9  rect.set_values (3,4);
10  cout << "area: " << rect.area() << endl;
11  return 0;
12 }
1 // CRectangle.h
2
3 class CRectangle {
    
    
4  int x, y;
5  public:
6  void set_values (int,int);
7  int area () {
    
    return (x*y);};
8 };
1 // CRectangle.cpp
2
3 #include "CRectangle.h"
4 using namespace std;
5
6 void CRectangle::set_values (int a, int b) {
    
    
7  x = a;
8  y = b;
9 }

2.对以上三个代码文件进行编译

g++ -o distributed rect CRectangle.cpp distributed rect main.cpp

3.验证实验结果

 ./distributed rect

可以得到

root@ubuntu:~# g++ -o distributed_rect CRectangle.cpp distributed_rect_main.cpp root@ubuntu:~# ./distributed_rect 
area:12

3.构造函数与析构函数

构造函数是一个类在被创建时自动调用的函数,析构函数是一个类被销毁时自动调用的函数。
其具体内容可以查看网页
http://www.cplusplus.com/doc/tutorial/classes

四、派生类

C++由于C的一个特性是可以分层定义类,可以定义继承父类属性的子类。这在代码的重复利用上有着巨大的优势。通过类的继承,可以使得MOOSapplication可以从MOOSApp中
1.建新一个程序文档derived,cpp
2.输入以下代码:

0 // Code example from: http://www.cplusplus.com/doc/tutorial/inheritance/
1 // derived classes
2 #include <iostream>
3 using namespace std;
4
5 class CPolygon {
    
    
6  protected:
7  int width, height;
8  public:
9  void set_values (int a, int b)
10  {
    
     width=a; height=b;}
11  };
12
13 class CRectangle: public CPolygon {
    
    
14  public:
15  int area ()
16  {
    
     return (width * height); }
17  };
18
19 class CTriangle: public CPolygon {
    
    
20  public:
21  int area ()
22  {
    
     return (width * height / 2); }
23  };
24
25 int main () {
    
    
26  CRectangle rect;
27  CTriangle trgl;
28  rect.set_values (4,5);
29  trgl.set_values (4,5);
30  cout << rect.area() << endl;
31  cout << trgl.area() << endl;
32  return 0;
33 }

3.要求增加一个派生类,使其输出为5

#include<iostream>
using namespace std;
class CPolygon{
    
    
    protected:
            int width,height;
    public:
            void set_values(int a,int b)
            {
    
    width=a;height=b;}
};
class CRectangle:public CPolygon{
    
    
        public:
                int area()
                {
    
    return (width*height);}
};
class CTriangle:public CPolygon{
    
    
        public:
                int area()
                {
    
    return (width*height/2);}
};
class CDiamond:public CPolygon{
    
    
        public:
                int area()
                {
    
    return width*height/4;}
};
int main()
{
    
    
        CRectangle rect;
        CTriangle trgl;
        CDiamond dia;
        rect.set_values(4,5);
        trgl.set_values(4,5);
        dia.set_values(4,5);
        cout << rect.area() << endl;
        cout << trgl.area() << endl;
        cout << dia.area() << endl;
        return 0;
}

结果如下`

root@ubuntu:~# ./derived 
20
10
5

总结

第二个实验到此结束,基本上把C++相关的基础知识复习了一遍。接下来是实验三,正式开始moos-ivp系统的编程实验。

猜你喜欢

转载自blog.csdn.net/weixin_44151170/article/details/108776839
今日推荐