当前位置:网站首页>Cases on classes and objects
Cases on classes and objects
2022-07-23 08:24:00 【Old fish 37】
Tips : When the article is finished , Directories can be generated automatically , How to generate it, please refer to the help document on the right
List of articles
Two cases about preliminary classes and objects
Design cube class

What we need to know is :
Then design a class
**1. Global function **
class Cube
{
public:
// Set length
void SetL(int L)
{
m_L = L;
}
// Get long
int GetL()
{
return m_L;
}
// Set the width
void SetW(int W)
{
m_W = W;
}
// Get the width
int GetW()
{
return m_W;
}
// Set high
void SetH(int H)
{
m_H = H;
}
// Get high
int GetH()
{
return m_H;
}
// Get the area
int CubeS()
{
return 2 * m_L * m_H + 2 * m_H * m_W + 2 * m_L * m_W;
}
int CubeV()
{
return m_L * m_W * m_H;
}
private:
int m_L;
int m_H;
int m_W;
};
// Global function
bool isSame(Cube& p1, Cube& p2)
{
if (p1.GetL()==p2.GetL() && p1.GetH()== p2.GetH() && p1.GetW() == p2.GetW())
{
return true;
}
return false;
}
int main()
{
// object 1
Cube cb1;
cb1.SetL(10);
cb1.SetW(10);
cb1.SetH(10);
// object 2
Cube cb2;
cb2.SetL(10);
cb2.SetW(10);
cb2.SetH(10);
cout << "cb1 The area of is :" << cb1.CubeS() << endl;
cout << "cb1 The volume of is :" << cb1.CubeV() << endl;
cout << "cb2 The area of is :" << cb2.CubeS() << endl;
cout << "cb2 The volume of is :" << cb2.CubeV() << endl;
bool ret = isSame(cb1, cb2);
if (ret == true)
{
cout << " Two cubes are equal " << endl;
}
else
{
cout << " It's not equal " << endl;
}
return 0;
}
2. Member functions
class Cube
{
public:
// Set length
void SetL(int L)
{
m_L = L;
}
// Get long
int GetL()
{
return m_L;
}
// Set the width
void SetW(int W)
{
m_W = W;
}
// Get the width
int GetW()
{
return m_W;
}
// Set high
void SetH(int H)
{
m_H = H;
}
// Get high
int GetH()
{
return m_H;
}
// Get the area
int CubeS()
{
return 2 * m_L * m_H + 2 * m_H * m_W + 2 * m_L * m_W;
}
int CubeV()
{
return m_L * m_W * m_H;
}
// Compare member functions
// Compare the parameters of the passed cube with the attributes in the existing class
bool isSame(Cube&p)
{
if (m_L== p.GetL() && m_H == p.GetH() &&m_W == p.GetW())
{
return true;
}
return false;
}
private:
int m_L;
int m_H;
int m_W;
};
// Global function
//bool isSame(Cube& p1, Cube& p2)
//{
// if (p1.GetL()==p2.GetL() && p1.GetH()== p2.GetH() && p1.GetW() == p2.GetW())
// {
// return true;
// }
// return false;
//}
int main()
{
// object 1
Cube cb1;
cb1.SetL(10);
cb1.SetW(10);
cb1.SetH(10);
// object 2
Cube cb2;
cb2.SetL(10);
cb2.SetW(10);
cb2.SetH(11);
cout << "cb1 The area of is :" << cb1.CubeS() << endl;
cout << "cb1 The volume of is :" << cb1.CubeV() << endl;
cout << "cb2 The area of is :" << cb2.CubeS() << endl;
cout << "cb2 The volume of is :" << cb2.CubeV() << endl;
bool ret = cb1.isSame(cb2);
if (ret == true)
{
cout << " Two cubes are equal " << endl;
}
else
{
cout << " It's not equal " << endl;
}
return 0;
}
The relationship between a point and a circle

From the topic we can deduce :
class Point
{
public:
// Set up X
void SetX(int x)
{
m_x = x;
}
// obtain X
int GetX()
{
return m_x;
}
// Set up Y
void SetY(int y)
{
m_y = y;
}
// obtain Y
int GetY()
{
return m_y;
}
private:
int m_x;
int m_y;
};
class Circle
{
public :
// Set the radius
void SetRadius(int r)
{
m_R = r;
}
// Get the radius
int GetRadius()
{
return m_R;
}
// Set the center
void SetCenter(Point p)
{
m_Center = p;
}
// Get the center
Point GetCenter()
{
return m_Center;
}
private:
int m_R;// radius
Point m_Center;// center of a circle // Another class is used here
};
// Judge
void isSame(Circle& p1, Point& q)
{
// Get the distance between the point and the center of the circle
int distance =
(p1.GetCenter().GetX() - q.GetX()) * (p1.GetCenter().GetX() - q.GetX()) +
(p1.GetCenter().GetY() - q.GetY()) * (p1.GetCenter().GetY() - q.GetY());
int distance2 = p1.GetRadius() * p1.GetRadius();// Square of radius
if (distance == distance2)
{
cout << " Point on the circle " << endl;
}
else if (distance < distance2)
{
cout << " The point is in the circle " << endl;
}
else
{
cout << " The point is outside the circle " << endl;
}
}
int main()
{
// Create circles
Circle c1;
c1.SetRadius(10);
// Create center point
Point p;
p.SetX(10);// Abscissa
p.SetY(0);// Ordinate
// Set the center
c1.SetCenter(p);
// Create points
Point p2;
p2.SetX(10);
p2.SetY(11);
// The comparison
isSame(c1, p2);
}
If there is a mistake , Your smile !
边栏推荐
- Several ways of QT thread exit
- 技术干货 | 数据处理好难?来看MindSpore提供的解决思路!
- Three cache strategies: cache side strategy, read/write through strategy, and write back strategy
- What if Alibaba cloud international forgets its member name or login password?
- TensorRT的插件实战(1)
- Redis persistence operation (RDB, AOF)
- The author believes that the development logic of the meta universe and the Internet is quite different in Chengdu
- RedisTemplate Pipeline 管道使用
- Flink原理初探和流批一体API(二)v2
- What should Alibaba cloud international account do if it receives an account risk notification?
猜你喜欢
随机推荐
技术干货 | 数据处理好难?来看MindSpore提供的解决思路!
获取一个控件宽度
关于常见排序的稳定性
Mria + RLOG 新架构下的 EMQX 5.0 如何实现 1 亿 MQTT 连接
Redis 事务学习有感
Comment synchroniser
Flink高级API(三)
Binary tree (learning daily)
There are 13 detailed methods for JMeter to view the response of the result tree!
EMQX v4.4.5 发布:新增排他订阅及 MQTT 5.0 发布属性支持
Flink implements a window accumulation function through processfunction and timer Ontimer
技术干货 | 基于MindSpore详解Perplexity语言模型评价指标
Use the same interface to realize different login methods
odbc excel--2022-07-21
LC:剑指 Offer 39. 数组中出现次数超过一半的数字
Xiaohongshu joins hands with HMS core to enjoy HD vision and grow grass for a better life
Send benefits again! Calendar applet source code
Promise (II)
I can't be angry with "voluntary salary reduction". I'm naked. I'm three times in four days. How can it end like this?
bryntum Kanban Task Board 5.1.0 JS 看板









