卜若的代码笔记-一周搞定树莓派-第六章:c++面向对象基础(一:定义基本对象)

1 你终于还是选择了cv侠

你开始觉得不甘,于是你开始去复习c++面向对象的基础

你打开了菜鸟教程,并进入了面向对象系列的学习

https://www.runoob.com/cplusplus/cpp-classes-objects.html

 

同时,你创建了一个QT的console程序

 

你开始了解c++里面的对象:

你发现,它和java有一些区别

class Box
{
   public:
      double length;   // 盒子的长度
      double breadth;  // 盒子的宽度
      double height;   // 盒子的高度
};

你学着它的样子定义了一个对象

你回想起头文件:定义变量和函数

你会想起源文件:实现函数

于是你创建一对头,源文件

box.h

#ifndef BOX_H
#define BOX_H


class Box
{
public:
    Box();
    int width;
    int height;
    int length;
public:
    int sum();
};

#endif // BOX_H

box.cpp

#include "box.h"

Box::Box()
{




};


int Box::sum(){



    return this->width+this->height+this->length;
}

你做了一个测试:

#include <QCoreApplication>
#include <iostream>
using namespace std;
#include"box.h"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

//    printf("hello world");
    Box box;

    box.height = 22;
    box.width = 10;
    box.length = 30;

    cout<<"sum of box ="<<box.sum();

    return a.exec();
}

你开始思考

Box box与java中Box box = new Box();

的区别

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值