首页 > 编程语言 >c++从入门到精通——类的设计

c++从入门到精通——类的设计

时间:2022-11-01 18:34:51浏览次数:44  
标签:精通 return 入门 int void c++ c2 c1 const


类的相同与否设计

#include<iostream>
using namespace std;
class Cube{
public:
void setL(int l){
m_L = l;
}
void setW(int W){
m_W = W;
}
void setH(int H){
m_H = H;
}
//获取长宽高
int getL() const
{
return m_L;
}
int getW() const
{
return m_W;
}
int getH() const
{
return m_H;
}
// 求面积
double calcaulateS() const{
return 2 * m_L*m_W + 2 * m_W*m_H + 2 * m_L*m_H;
}
//求体积
int calculateV() const
{
return m_L * m_W * m_H;
}

//利用成员函数 判断立方体是否相等
bool compareCubeByClass( Cube &c ) const
{
return m_L == c.getL() && m_W == c.getW() && m_H == c.getH();
}

private:
int m_L; //长
int m_W; //宽
int m_H; //高

};
bool compareCude(Cube &c1,Cube &c2){
/*if (c1.getL() == c2.getL() && c1.getW() == c2.getW() && c1.getH() == c2.getH())
{
return true;
}
return false;*/
return c1.getL() == c2.getL() && c1.getW() == c2.getW() && c1.getH() == c2.getH();
}
void test01(){
Cube c1{};
c1.setL(10);
c1.setW(10);
c1.setH(10);
cout << "c1面积为: " << c1.calcaulateS() << endl; //600
cout << "c1体积为: " << c1.calculateV() << endl; //1000

Cube c2{};
c2.setL(10);
c2.setW(10);
c2.setH(10);


//利用全局函数判断 c1 和 c2是否相等
bool ret = compareCude(c1, c2);
if (ret)
{
cout << "c1 与 c2 相等" << endl;
}
else
{
cout << "c1 与 c2 不相等" << endl;
}


//利用成员函数 判断 c1 和 c2是否相等
ret = c1.compareCubeByClass(c2);
if (ret)
{
cout << "成员函数判断:c1 与 c2 相等" << endl;
}
else
{
cout << "成员函数判断:c1 与 c2 不相等" << endl;
}
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}

类的点圆案例设计

main.cpp

#include <iostream>
#include "point.h"
#include "circle.h"
//利用全局函数 判断点和圆关系
void isInCircle( Circle c , Point p )
{

int distance = (c.getCenter().getX() - p.getX())*(c.getCenter().getX() - p.getX()) + (c.getCenter().getY() - p.getY()) *(c.getCenter().getY() - p.getY());

int rDistance = c.getR() * c.getR();

if (distance == rDistance)
{
cout << "点在圆上" << endl;
}
else if (distance > rDistance)
{
cout << "点在圆外" << endl;
}
else
{
cout << "点在圆内" << endl;
}

}
void test01()
{
Point p;
p.setX(10);
p.setY(9);

Circle c;
Point pCenter;
pCenter.setX(10);
pCenter.setY(0);
c.setCenter(pCenter);
c.setR(10);

isInCircle(c, p);

c.isInCircleByClass(p);

}
class A
{
virtual void func() = 0;
};
class B :public A
{

};
class C :public B
{
};

int main(){

test01();

system("pause");
return EXIT_SUCCESS;
}

circle.h

//
// Created by CWF on 2022/10/5.
//

#ifndef CODE_C___CIRCLE_H
#define CODE_C___CIRCLE_H
#include <iostream>
#include "point.h"
using namespace std;
class Circle{
public:
void setR(int r);
int getR()const;
void setCenter(Point &p);
Point getCenter() const;
//判断点和圆关系
void isInCircleByClass(Point p);

private:
int m_R{0};
Point m_Center;
};
void Circle::setR(int r) {
this->m_R = r;
}
int Circle::getR() const {
return this->m_R;
}
Point Circle::getCenter() const{
return this->m_Center;
}
void Circle::setCenter(Point &p) {
m_Center = p;
}
//判断点和圆关系
void Circle::isInCircleByClass(Point p)
{

int distance = (m_Center.getX() - p.getX())*(m_Center.getX() - p.getX()) + (m_Center.getY() - p.getY()) *(m_Center.getY() - p.getY());

int rDistance = m_R * m_R;

if (distance == rDistance)
{
cout << "is" << endl;
}
else if (distance > rDistance)
{
cout << "out" << endl;
}
else
{
cout << "in" << endl;
}
}

#endif //CODE_C___CIRCLE_H

point.h

//
// Created by CWF on 2022/10/5.
//

#ifndef CODE_C___POINT_H
#define CODE_C___POINT_H
#include <iostream>
class Point{
public:
void setX(int x){
this->m_X = x;
}
void setY(int y){
this->m_Y = y;
};
int getX() const;
int getY() const;
private:
int m_X;
int m_Y;

};
int Point::getX() const{
return this->m_X;
}
int Point::getY() const{
return this->m_Y;
}

#endif //CODE_C___POINT_H


标签:精通,return,入门,int,void,c++,c2,c1,const
From: https://blog.51cto.com/u_13859040/5814688

相关文章