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

目录

[9.1.4 设计立方体类](#9.1.4 设计立方体类)

​编辑

[9.1.5 成员函数在类的外部实现](#9.1.5 成员函数在类的外部实现)

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

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


9.1.4 设计立方体类

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

注意点:

完整代码:

cpp 复制代码
#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;
}
相关推荐
小柳不言败几秒前
面试题总结一
面试
埃菲尔铁塔_CV算法1 分钟前
深度学习驱动下的目标检测技术:原理、算法与应用创新(二)
深度学习·算法·目标检测
wuqingshun3141594 分钟前
经典算法 (A/B) mod C
c语言·开发语言·c++·算法·蓝桥杯
白杆杆红伞伞7 分钟前
04_决策树
算法·决策树·机器学习
半青年9 分钟前
Qt图表库推荐指南与分析
c语言·开发语言·javascript·c++·qt·信息可视化
爱coding的橙子11 分钟前
算法刷题Day9 5.18:leetcode定长滑动窗口3道题,结束定长滑动窗口,用时1h
算法·leetcode·职场和发展
姬公子52115 分钟前
leetcodehot100刷题——排序算法总结
数据结构·c++·算法·排序算法
艾米莉亚糖26 分钟前
解决qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed
开发语言·qt·ssl
AndrewHZ37 分钟前
【图像处理基石】OpenCV中都有哪些图像增强的工具?
图像处理·opencv·算法·计算机视觉·滤波·图像增强·颜色科学
她说彩礼65万42 分钟前
C# 中的锁
开发语言·c#