Introduction to C++ Class
Get acquainted with C++ Class declaration and definition
Overview:
One of the main purpose of C++ programming is to add object orientation to the C programming language. To describe it in a sentence, Object Oriented Programming (OOP) is a programming paradigm that prioritizes defining a data over manipulation of it. Getting started with OOP first requires getting acquainted with C++ classes and objects.
Classes and Objects:
Classes are used to implement object orientation with C++. Basically, classes are data structures created by a programmer to better suit the data needs of a particular program. This is why, classes are also often referred to as user-defined types.
What are objects then?
An object can be defined as an instance of it’s class. We can also view classes and objects in terms of data types and variable names, where the class name is the data type and the object is the variable name.
Book book;
Here, book
is an object of a class Book
.
How to declare a class:
Declaring a class is essentially defining a blueprint for a data structure. Classes are defined using the keyword class
with the following syntax:
class class_name {
access specifier_1:
member_1;
access_specifier_2:
member_2;
...
} object_names;
This looks very familiar.
Yes, the outline for a class declaration looks very similar to a data structure declaration. Let’s look at each keyword more closely:
class
is the keyword needed to declare a new Class.class_name
should be a name that we can use later to declare an object of its type.member
can be a native data type of a user-defined type. Functions can also be declared as members.object_names
should be valid identifiers for objects of the typeclass_name
.
Aren’t you missing something?
Yes, I did that on purpose because we need to discuss access specifiers in more detail_._
Access Specifiers:
Access specifiers define how members of a class can be accessed. There are three access specifiers for a class in C++:
public
— The public members are accessible from outside the class through an object of the class, typically with the use of the dot operator.protected
— The protected members can be accessed from outside the class but only in a class derived from it.private
— The private members are only accessible from within the class or in it’s member functions. Using a dot operator to access private members will result in the compiler throwing an error.
Let’s look at a fun example to see all this in action:
Fun Example:
#
using namespace std;class Book {
private:
int year;
double price;
public:
string title;
void printBook();
void setPrice(double p);
void setYear(int y);
} book;void Book::setPrice(double p) {
price = p;
}void Book::setYear(int y) {
year = y;
}void Book::printBook() {
cout << "Title: " << title << "\n";
cout << "Price: " << price < "\n";
cout << "Year: " << year << "\n";
}int main() {
book.title = "hooked";
book.setYear(2013);
book.printBook();
return 0;
}
Let’s break this example down into code blocks and discuss each of them individually:
Class Declaration:
class Book {
private:
int year;
double price;
public:
string title;
void printBook();
void setPrice(double p);
void setYear(int y);
} book;
This is a typical class declaration. Following the syntax we discussed above, we have our class
keyword declaring a class named Book
.
Inside the class declaration, we have private and public members. Notice that we are not using any protected members because we would not need them unless we create another class derived from this class. We will discuss more about it on later tutorials.
Private Access Specifier:
private:
int year;
double price;
Here, we have two private variables. These private variables can only be accessed by the members of the class within the class. In C++ classes, if we don’t specify an access specifier, the default access would always be private. Therefore, you can write this code block as following and it would not make any difference:
class Book {
int year;
double price;
public:
string title;
void setPrice(double p);
void setYesar(int y);
void printBook();
} book;
Public Access Specifier:
public:
string title;
void setPrice(double p);
void setYesar(int y);
void printBook();
Here, we have one public variable and three public functions. These function can also be called class methods.
string title;
Unlike the private variables, the public variable title
can be accessed form an object of the class using the dot operator. On the other hand, the private variables cannot be accessed from an object, therefore, we need public methods in the class to access and modify the private variables. In our example, we have setPrice()
and setYear()
to set values for their respective private variables and printBook()
to read and display the values of the private variables.
void setPrice(double p);
void setYesar(int y);
void printBook();
Defining Methods:
Class methods are basically just functions. The distinction being that for a function to become a method, it needs to be bound to a class. I guess you could then define methods as functions bound to a class.
void Book::setPrice(double p) {
price = p;
}
First thing to notice in this code block is ::
. This is the C++ scope resolution operator. This operator can be used, as in the example, to bind a function to a class.
Next thing to notice is the assignment of price
. price
is a private variable of the class. The primary reason we wrote the method setPrice()
is to set the private price
variable because we cannot directly access the variable in our program. Since we bound the function to the class, the method now has access to all private members of the particular class, giving us the liberty to change or print the member variables using the member functions.
Dot Operator:
Dot operators .
are used to access the public members of an object. In this code block, we can use the dot operator to access public variables of the book object and use the public methods to access the private variables of the object.
int main() {
book.title = "hooked";
book.setYear(2013);
book.printBook();
return 0;
}
In a nutshell:
Classes in C++ are central for object orientation. Object orientation prioritizes the definition of a data structure, therefore we must always define a class before it’s use. The class can be defined according to the needs of a particular program or the same class can be used in multiple programs. This is just an introduction to classes and there are more basic functionality to learn. Stay tuned for more tutorials for C++ classes.