首页 > 编程语言 >C++对象和类

C++对象和类

时间:2023-04-14 17:44:48浏览次数:35  
标签:const frameNum 对象 Global len char int C++

一、类的声明

//global.h -- define for all project
//version 0.0
#ifndef GLOBAL_H_    //防止重定义
#define GLOBAL_H_

class Global        //定义类
{
public:                //使用类对象的程序都可以直接访问公有部分
    /*定义在类声明中的函数称为内联函数,仅声明在类中的函数不是内联函数,
    但定义时加上"inline"就是内联函数:inline int Global::read_sound(){}*/
    int read_sound(const char* fileName, char* sound, int len);        
    int read_gray_dat(const char* fileName, uint16_t** Gray, int frameNum, int len);
    int read_depth_dat(const char* fileName, uint16_t** Depth, int frameNum, int len);
    int read_cloud_dat(const char* fileName, float** PointCloud, int frameNum, int len);
    int read_RGB_dat(const char* fileName, uint8_t** RGB, int frameNum, int len);

    /*构造函数的作用:构造新对象、将值赋给他们的数据成员;构造函数名为Global(),
    没有返回值,也没有声明类型;程序在声明对象时会自动调用构造函数*/
    Global(int height, int width);

    /*构造函数的作用:构造新对象、将值赋给他们的数据成员;构造函数名为~Global(),
    没有返回值,也没有声明类型和参数;作用是完成清理工作,释放内存*/
    ~Global();

    /*
    const 成员函数:
    当类的声明为:const Global obstacle = Global(480, 640);    时不能调用成员函数,因为不能保证私有成员数据
    是否会被改变,若要被调用此时函数的声明应为:int read_sound(const char* fileName, char* sound, int len) const;    
    函数的定义为:int Global::read_sound(const char* fileName, char* sound, int len) const{}
    */

    /*this指针:this指针指向调用对象*/
    const Global& compare(const Global& s) const;

private:            //访问私有部分只能通过公有成员函数或者友元函数,不写关键字的声明默认为是private
    int m_height;
    int m_width;
    static const int Months = 12;//类中创建数组方式
    double costs[Months];

protected:
};
#endif

二、成员函数的定义

//global.h -- define global function
//version 0.0
/*
定义成员函数时使用作用域解析运算符(::)来表示函数;
类方法可以访问类的private组件;
*/

#include <iostream>
#include "global.h"

using namespace std;

//构造函数
Global::Global(int height, int width) {
    m_height = height;
    m_width = width;
}
/*
构造函数定义时如果不设置参数数,则可以在声明对象时直接调用:Global obstacle;
Global::Global() {
    m_height = 480;
    m_width = 640;
}
*/


//析构函数,它的调用时间由编译器决定
Global::~Global() {
    cout << "game over" << endl;
}

const Global& Global::compare(const Global& s) const
{
    if (s.m_height > m_height) {
        return s;
    }
    else
    {
        return *this;
    }
}

//读取超声波数据
int Global::read_sound(const char* fileName, char* sound, int len)
{
    if (NULL == fileName)
    {
        return 0;
    }
    //--------------------------------------------------------------------------------
    //open file
    FILE* fFileID = fopen(fileName, "rb");
    if (NULL == fFileID)
    {
        cout << "Can't open the file." << endl;
        exit(EXIT_FAILURE);
    }

    fread(sound, sizeof(char), len, fFileID);
    fclose(fFileID);

    return 1;
}

 三、类的调用

#include "global.h"
#include <iostream>
#include <fstream>
#include <math.h>
#include <iomanip>
#include <opencv2\opencv.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\opencv.hpp>

int main() {
    //初始化全局参数
    char CloudFile[256] = { 0 };
    char GrayFile[256] = { 0 };
    char RGBFile[256] = { 0 };
    char UsoundFile[256] = { 0 };
    char DepthFile[256] = { 0 };
    int width = 320;
    int height = 240;
    int len = width * height;
    int frameNum = 20;

    //开辟多帧全局空间
    char* sound = (char*)malloc(sizeof(char) * 3);            //单帧超声波数据
    uint16_t* GrayData = (uint16_t*)malloc(sizeof(uint16_t) * len * frameNum);
    float* CloudData = (float*)malloc(sizeof(COOR3D32F) * len * frameNum);
    uint16_t* DepthData = (uint16_t*)malloc(sizeof(uint16_t) * len * frameNum);
    uint8_t* RGBData = (uint8_t*)malloc(sizeof(uint8_t) * len * frameNum * 1.5);
    uint16_t** Gray_raw = (uint16_t**)malloc(sizeof(uint16_t*) * frameNum);
    uint16_t** Depth_raw = (uint16_t**)malloc(sizeof(uint16_t*) * frameNum);
    float** Point_raw = (float**)malloc(sizeof(float*) * frameNum);
    uint8_t** RGB_raw = (uint8_t**)malloc(sizeof(uint8_t*) * frameNum);
    //Mat img(200, 200, CV_8UC1);
    //img = Thred();

    int data_long = 3 * len;
    for (int i = 0; i < frameNum; i++)
    {
        Gray_raw[i] = (uint16_t*)(&GrayData[i * len]);
        Depth_raw[i] = (uint16_t*)(&DepthData[i * len]);
        Point_raw[i] = (float*)(&CloudData[i * 4 * len]);
        RGB_raw[i] = (uint8_t*)(&RGBData[i * data_long]);
    }

    char path[] = "E:\\check_GitCode\\obstacle\\XB_test_bug_data\\室内\\室内-10x5cm_低反\\室内-10x5低反-1421mm(及之后距离)识别不到\\";
    char point[] = "point_cloud.dat";
    char usound[] = "usound.dat";
    //char gray[] = "gray.dat";
    char depth[] = "depth.dat";
    //char rgb[] = "RGB_DATA.dat";

    sprintf(CloudFile, "%s%s", path, point);
    //sprintf(RGBFile, "%s%s", path, rgb);
    //sprintf(GrayFile, "%s%s", path, gray);
    sprintf(DepthFile, "%s%s", path, depth);
    sprintf(UsoundFile, "%s%s", path, usound);

    Global obstacle = Global(480, 640);        //声明一个Global类对象,叫obstacle,显式调用构造函数
    //也可也用列表初始化:Global obstacle = {480, 640};    Global avoid{480};
    Global avoid(480, 640);                    //声明一个Global类对象,叫avoid,隐式调用构造函数
    Global* pstock = new Global(480, 640);    //使用new构造函数,这种情况下对象没有名称,使用pstock指针来管理对象
    obstacle.read_sound(UsoundFile, sound, 3);        //成员函数调用
    obstacle.read_cloud_dat(CloudFile, Point_raw, frameNum, len);
    obstacle.read_depth_dat(DepthFile, Depth_raw, frameNum, len);
    //read_RGB_dat(RGBFile, RGB_raw, frameNum, width, height, data_long);
    //read_gray_dat(GrayFile, Gray_raw, frameNum, width, height, len);

    /*对象数组:一次声明多个类对象*/
    const int num = 4;
    Global ob_avoid[num] = {
        Global(480, 320),
        Global(480, 320),
        Global(480, 320),
        Global(480, 320)
    };
    ob_avoid[0].read_cloud_dat(CloudFile, Point_raw, frameNum, len);

 

标签:const,frameNum,对象,Global,len,char,int,C++
From: https://www.cnblogs.com/okmai77xue/p/17319056.html

相关文章

  • 功能不够用?使用C++编写通达信插件及接入Python(一)
    第一次尝试,参照:http://www.xiaoyunyun.net/index.php/archives/53.html 和 https://blog.csdn.net/wiowei/article/details/121466094在绑定DLL环节失败了第二次尝试:参照:https://zhuanlan.zhihu.com/p/5698198681.修改VS2019,勾选 windows10SDK2.用Visualstudio打开......
  • 列举说明Python同Java及C++的不同之处
    首先是C++C++是在C语言的基础上发展起来的,他包含了C语言的所有内容。同时,也引入了面向对象的概念。优点:1、他包含了C语言的内容,包括指针,使得C++在执行效率上特别的高效。2、引入面向对象的概念,使得开发效率提高。3、提供了很多的库,具有较好的封装性和移植性(代码)。缺点:1、C++比较难......
  • Object对象转为Date对象
    SimpleDateFormatdateFormat1=newSimpleDateFormat("yyyy-MM-dd");Objectobject=dgDetails.get("time");Datedate=null;try{date=dateFormat1.parse((String)object);}catch(ParseExceptione){e.printStackTrace();}SimpleDateF......
  • 【C/C++】assert实现
     #defineMLA_ASSERT(expr)(void)((!!(expr))||(printf("mlaassertfail:\"%s\"@%s,%u",#expr,__FILENAME__,__LINE__),printf("\r\n"),abort()//usage:MLA_ASSERT(pointer!=NULL)#defineMLA_ASSERT(expr)(vo......
  • JavaScript 之 JSON [4] parse()和stringify() -JSON字符串和JavaScript对象数据之间
    JavaScript之JSON[4]parse()和stringify()-JSON字符串和JavaScript对象数据之间的相互转换1、JSON.parse()JSON.parse()方法用于将一个JSON字符串解析为一个JavaScript对象。JSON字符串必须使用双引号包括属性名和字符串值,不能使用单引号或无引号。语法:JSON.parse(text,r......
  • 对象的深拷贝
    CSDN:https://blog.csdn.net/cc18868876837/article/details/114918262掘金:https://juejin.cn/post/7207090090101866557#heading-16functiondeepClone(obj,cloneObj){varcloneObj=cloneObj||{};for(letiinobj){......
  • java 如何判断对象是否是垃圾
    引用计数法给对象添加一个引用计数器,当对象增加一个引用时计数器加1,引用失效时计数器减1。引用计数为0的对象就是垃圾可被回收比如a线程中某个栈帧使用某个对象,这个对象的引用计数器+1,调用结束,引用计数器-1这个方法实现简单,效率高,但是并不可取,当循环引用时,引用计数器永......
  • Java SpringBoot 中,动态执行 bean 对象中的方法
    根据不同的条件,调用不同的bean对象,执行对象中的方法SpringUtils工具类packagecom.vipsoft.web.utils;importcn.hutool.core.util.ArrayUtil;importorg.springframework.aop.framework.AopContext;importorg.springframework.beans.BeansException;importorg.sprin......
  • C++ 学习 第四天
    流程控制练习题1.控制台输入一个数(不多于五位),并将每一位上的数进行输出2.一个球从100米的高度自由落下,每次落地后反跳回原高度的一半;再落下,求他在第10次落地时,共经过了多少米?第十次反弹多高? 3.接上题,球经历多少次反弹后,反弹高度小于1米?4.计算1000内的完数。完数:一个数恰好......
  • C++的引用变量作为函数参数
    一、问题引入在C++的中新增一个特性:按引用传递变量,虽然与按传递变量的地址可以实现相同的结果,但引用有其独特的地方。引用传递:引用经常被用作函数参数,使得函数中的变量名成为调用程序中的变量的别名。通过引用变量用作参数,函数将使用原始数据,而不是其副本。二、解决过程举......