How to define class in cpp?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to define class in cpp??

24th Jan 2019, 7:07 AM
Kishan Gadhiya
5 Answers
+ 2
Take this example: #include<iostream> using namespace std; class rectangle { private: int len,br; public: void set(int l,int b) { len=l; br=b; } void disp() { cout<<endl<<len<<endl<<br; } }; int main() { rectangle r1, r2 ; r1.set(20,15); //sets the value of r1 r2.set(25,20); r1.disp();// displayes the len and br of r1 r1 .disp(); return 0; }
24th Jan 2019, 7:40 AM
Sahil Bhakat
Sahil Bhakat - avatar
+ 7
It is covered in the lesson: https://www.sololearn.com/learn/CPlusPlus/1711/ P.S. Specify the language in "Relevant Tags" please ...
24th Jan 2019, 7:36 AM
Ipang
+ 1
You should go through the lessons of class and constructer and destructers
24th Jan 2019, 7:49 AM
Sahil Bhakat
Sahil Bhakat - avatar
0
What is set and disp???
24th Jan 2019, 8:00 AM
Kishan Gadhiya
0
set and disp are the function that can only read or write the private variables(len & br). You can define as many functions as you want or as program requires in the public segment As for some knowledge The private segment cannot be accessed outside the class,it is called data hiding.
24th Jan 2019, 8:30 AM
Sahil Bhakat
Sahil Bhakat - avatar