【带头学C++】----- 九、类和对象 ---- 9.1 类和对象的基本概念----(9.1.4---9.1.6)

目录

9.1.4 设计立方体类

​编辑

9.1.5 成员函数在类的外部实现

9.1.6 类在其他源文件的实现步骤(实现类在不同文件的实现,后续引出构造函数) 

注意:类定义在同文件testclass.h中,而testclass.cpp是用来实现(声明)类的成员函数文件。


9.1.4 设计立方体类

现在如下图所示题目,设计一个立方体类,并且可以求出立方体的面积、体积,并最后判断是否相等,这里严格来说应该是设计一个长方体,立方体是长宽高都相等才是。

注意点:

完整代码:

#include <iostream>
#include <string>

class Cube{
private:
    int mLenth;//长
    int mWidth;//宽
    int mHeight;//高
public:
    void cubeInit(int ml,int mw,int mH){
        mLenth = ml;
        mWidth = mw;
        mHeight = mH;
    }
    //获取长宽高
    int getL(void){
        return mLenth;
    }
    int getW(void){
        return mWidth;
    }
    int getH(void){
        return mHeight;
    }
    //计算面积
    int getcubeS(){
        return (mLenth*mWidth + mLenth*mHeight + mWidth*mHeight)*2;
    }
    //计算体积
    int getcubeV(){
        return mLenth*mWidth*mHeight;
    }
    
    bool compareCube2(Cube &ob2){
        if(mLenth == ob2.getL() && mWidth == ob2.getW() && mHeight == ob2.getH()){
            return true;
        }
        return false;
    }
};


bool compareCube1(Cube &ob1,Cube &ob2)
{
    if(ob1.getL() == ob2.getL() && ob1.getH() == ob2.getH() && ob1.getW() == ob2.getW()){
        return true;
    }else{
        return false;
    }
}

void test04(){
    Cube ob1;
    ob1.cubeInit(10,20,30);
    cout << "面积:" <<ob1.getcubeS()<<endl;
    cout << "体积:" <<ob1.getcubeV()<<endl;

    Cube ob2;
    ob2.cubeInit(5,6,7);
    cout << "面积:" <<ob2.getcubeS()<<endl;
    cout << "体积:" <<ob2.getcubeV()<<endl;
    //全局函数在类外部实现访问局部变量(公有方法访问私有的成员)
    if(compareCube1(ob1,ob2))
    {
        cout << "相等" <<endl;
    }else{
        cout << "不相等" <<endl;
    }
    //局部成员函数在类内部调用(直接使用对象名进内部比较)
    if(ob1.compareCube2(ob2))
    {
        cout << "相等" <<endl;
    }else{
        cout << "不相等" <<endl;
    }
}
int main() {
    test04();
    return 0;
}

9.1.5 成员函数在类的外部实现

9.1.6 类在其他源文件的实现步骤(实现类在不同文件的实现,后续引出构造函数) 

在Qt新建一个项目

 

 

 

 这里可以直接选择在.cpp文件实现,注意,编写代码要严格区分大小写。

注意:类定义在同文件testclass.h中,而testclass.cpp是用来实现(声明)类的成员函数文件。

代码:

testclass.h

#ifndef TESTCLASS_H
#define TESTCLASS_H


class TestClass
{
private:
    int mA;
public:
    void setA(int a);
    int getA();
};

#endif // TESTCLASS_H

testclass.cpp

#include "testclass.h"


void TestClass::setA(int a)
{
    mA = a;
}

int TestClass::getA()
{
    return mA;
}

main

#include "testclass.h"
void test05(){
    TestClass ob1;
    ob1.setA(111);
    cout << "ob1 = " <<ob1.getA()<<endl;
}
int main() {
    test05();
    return 0;
}


相关推荐

  1. C++ 对象

    2023-12-07 04:34:04       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-07 04:34:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-07 04:34:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-07 04:34:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-07 04:34:04       20 阅读

热门阅读

  1. 利用 Python 进行数据分析实验(二)

    2023-12-07 04:34:04       43 阅读
  2. linux系统调用介绍

    2023-12-07 04:34:04       42 阅读
  3. Vue的methods中定时器的变量报错问题

    2023-12-07 04:34:04       43 阅读
  4. C++ day50 买卖股票最佳时机

    2023-12-07 04:34:04       41 阅读
  5. linux优化-平均负载率

    2023-12-07 04:34:04       39 阅读
  6. 数据结构 / 队列 / 循环队列 / 结构体定义和创建

    2023-12-07 04:34:04       46 阅读
  7. vue的模板语法

    2023-12-07 04:34:04       43 阅读
  8. 使用右值常量进行测试的boost::foreach模块

    2023-12-07 04:34:04       43 阅读
  9. Vue经典面试题源码级分析【一】

    2023-12-07 04:34:04       44 阅读
  10. C#学习相关系列之数组---常用方法使用(二)

    2023-12-07 04:34:04       42 阅读