From the course: Object-Oriented Programming with C++
Defining classes - C++ Tutorial
From the course: Object-Oriented Programming with C++
Defining classes
- [Instructor] A class is a fundamental building block of object-oriented programming in C++. It allows you to group related data and functions into a single cohesive unit. In this video, I'll walk you through creating a basic class in C++. First, we'll include the iostream header needed for output operations. Next, we'll define a class called Review using the class keyword. The most basic review should have at least a rating, a title, and some text. We'll add three member variables to represent these rating. For the review score, let's make it an unsigned integer. Title, for the review's heading, I'll define it as a string. And text for the body content, also a string. Just like for any variable, we specified the type followed by the variable name. These variables represent the data each review can hold. The member variables are currently private by default. We'll talk about excess specifiers in detail later. After defining the member variables, let's also add a member function called displayDetails. It doesn't return anything and it doesn't take any input parameters, and because it doesn't modify any of the member variables, it should be a const function. This function prints out the details of the review, including its rating, title, and text. I'll use std::cout. First, we'll display the rating out of five, followed on a new line by the review's title, and on another line the review's text. And let's close this line. So let's revisit what we've got so far. We define the class, added member variables for storing its data and a member function that operates on the class's data. Next up, we'll talk about exospecifiers.
Contents
-
-
-
Defining classes2m 38s
-
(Locked)
Access specifiers3m 33s
-
(Locked)
Separating class declaration from class definition3m 42s
-
(Locked)
Creating and using objects2m 29s
-
(Locked)
Constructors and destructors5m 23s
-
(Locked)
Challenge: Implementing a smart home thermostat class1m 55s
-
(Locked)
Solution: Implementing a smart home thermostat class1m 50s
-
-
-
-
-
-