当前位置:网站首页>Using member variables and member functions of a class
Using member variables and member functions of a class
2022-06-30 14:49:00 【I would, I think】
Use a : Object name . Member name
#include <iostream>
using namespace std;
class crectangle
{
public:
int w, h;//w and h Is a member variable
void Init(int w_, int h_)// This function is used to initialize , Set width and height , So this function is also a member function
{
w = w_;
h = h_;
}
int area()// Here are three member functions
{
return w * h;
}
int perimeter()
{
return 2 * (w + h);
}
};
int main()
{
int w1, h1;
cin >> w1 >> h1;
crectangle r;//r It's an object
r.Init(w1, h1);// call Init Member functions
//r.Init explain Init The function acts on r The above object , Enter Init In member function ,w1 and h1 All belong to r This object ,
// It also indicates that this member function will affect r Inside w1 and h1
cout << r.area() << endl << r.perimeter();
return 0;
}Use two : The pointer —> Member name
#include <iostream>
using namespace std;
class crectangle
{
public:
int w, h;//w and h Is a member variable
void Init(int w_, int h_)// This function is used to initialize , Set width and height , So this function is also a member function
{
w = w_;
h = h_;
}
int area()// Here are three member functions
{
return w * h;
}
int perimeter()
{
return 2 * (w + h);
}
};
int main()
{
int w, h;
crectangle r1,r2;
crectangle* p1 = &r1;
crectangle* p2 = &r2;
p1->w = 5;// to r1 Medium w The assignment is 5,h The assignment is 6
p1->h = 6;
p2->Init(6, 7);//Init It works on p2 Point to the object , namely r2
cout << r1.area() << endl << r1.perimeter()<<endl;
cout << r2.area() << endl << r2.perimeter();
return 0;
}
Use three : Reference name . Member name
int main()
{
int w, h;
crectangle r1;
crectangle& rr = r1;//rr Refer to the r1, therefore rr and r1 It's actually the same thing
rr.w = 5;//rr The value of has changed ,r1 And the value of
rr.Init(5, 4);
cout << r1.area() << endl << r1.perimeter() << endl;
}
边栏推荐
- PS tip: the video frame to Layer command cannot be completed because dynamiclink is not available
- Laravel8 custom log directory, rename
- Greedy two-dimensional array sorting
- CCF sequence segmentation (Full Score code + problem solving idea) 201509 -1
- 机械工程师面试的几个问题,你能答上来几个?
- After the MySQL service on the local computer is started and stopped, some services will automatically stop when they are not used by other services or programs
- XSS challenge (1-5) more detailed answers
- JS array sorting method summary
- Programming exercises: whole point and circle (solution ideas and code implementation)
- Experiment 2: stack
猜你喜欢

Repair of incorrect deletion of win10 boot entry

PS tip: the video frame to Layer command cannot be completed because dynamiclink is not available

Solve the problem that codeblocks20.03 on win11 cannot run for the first time

ctfshow nodejs

2021 geek challenge Web

【BUUCTF】 EasySql

【BUUCTF】 Have Fun

Error $(...) size is not a function
![[buuctf] [actf2020 freshman competition]exec1](/img/af/22051a5feb3c1f6d7201a483bde127.jpg)
[buuctf] [actf2020 freshman competition]exec1

Ctfshow getting started with the web (ThinkPHP topic)
随机推荐
[buuctf] [geek challenge 2019] secret file
Att & CK red team evaluation field (I)
数控加工中心打刀缸工作原理及故障处理
Svn password forgetting solution
Laravel RBAC laravel permission use
JS time conversion standard format, timestamp conversion standard format
Working principle and fault treatment of cutting cylinder in CNC machining center
浅析卧式加工中心上不规则台阶孔存在问题
Lihongyi machine learning 2020 homework summary
Summary of use of laravel DCAT admin
The first dark spring cup dnuictf
How to use Alibaba Vector Icon
Is it troublesome for CITIC futures to open an account? Is it safe? How much is the handling charge for opening an account for futures? Can you offer a discount
IO interview questions
左旋梯形螺纹的编程
C language & the difference between the address pointed to and the address pointed to by the pointer
Programming exercises: special numbers (problem solving ideas + code implementation)
CCF sequence segmentation (Full Score code + problem solving idea) 201509 -1
Judgment of deep learning experiment results
Add attributes to multimode