Lesson 15 of 24

Classes and Objects

10 min 30 XP

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.

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

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.