Lesson 15 of 24

Classes மற்றும் ObjectsClasses and Objects

10 min 30 XP

Class ஒரு வரைபடம்: data members மற்றும் member functions. private உறுப்புகளை class-க்கு வெளியே அணுக முடியாது (encapsulation); public methods மூலம் மட்டும்.

A class is a blueprint with data members and member functions. private members can't be touched from outside the class (encapsulation); public methods are the way in.

Example: edit me and press Run

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.

class-ல் உறுப்புகள் இயல்பாக private; struct-ல் இயல்பாக public. இதுதான் C++-ல் class vs struct-ன் ஒரே வேறுபாடு.

Members of a class are private by default; members of a struct are public by default. That's the only difference between them in C++.

Challenge

+30 XP

Rectangle class எழுதுங்கள்: private length, width; public set(int l, int w), area(), perimeter(), isSquare(). main()-ல் l, w படித்து print செய்யுங்கள்: Input: 4 4 Output: Area: 16 Perimeter: 16 Square: yes

Write class Rectangle with private length and width and public set(int l, int w), area(), perimeter() and isSquare(). In main(), read l and w and print: Input: 4 4 Output: Area: 16 Perimeter: 16 Square: yes

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.