본문 바로가기

Programming/C++ Basic

[Basic C++] 13 – Data Structure in C++ : What is a CLASS, definition, and explanation

In this post, I am going to talk about "Class" in C++. A class is the most fundamental element of "Objective-Oriented Programming" (OOP). At a time when C was invented by Dennis Ritchie in 1972, OOP was not a major concept of programming, thus everyone learned "Procedure-Oriented Programming" (POP). However, from early 1990, OOP became one of the major programming concepts. Now we do not only have C for POP but also Java, Python, and C# for OOP. You might or might not be familiar with these terminologies, but I will write a post about them soon.

 

POP, also known as structured programming, is a way of constructing a program based on procedure calls which are a series of orders or commands to a target machine. The target machine, in this case, it means your computer CPU, executes those commands step by step from the top to the bottom. This is also how we read and write.

 

On the other hand, OOP is a way of constructing a program based on objects. Objects are data types representing certain functionalities and they are categorized and form a hierarchy. As I said in the earlier post, objects are like categories. Cars are vehicles, vehicles are machines, machines are made of steel, something like that. Objects are created to describe abstract concepts and they can be modified or transformed into different forms freely.


Table of contents

1. How to write a class in C++

2. Access specifier

3. Four characteristics of a class

 


1. How to write a class in C++

The syntax of a class has no difference from a data structure in C++. The keyword "class" is used to define a class. That's pretty much everything for the basic syntax. 

 

Syntax of Class

class identifier
{
private:  // Access specifier
	member1; // Class member
protected:
	member2;
public:
	member3;
} object;

identifier object1;
object1.member3;
identifier object2;
object2.member3

 

A class needs an identifier just like other data types. What is important here is access to specifiers and class members. Class members are the same as members in a data structure. They can be data types, functions, data structures, and even classes.

 

Access specifiers, on the other hand, are what we didn' cover in the early post. They are related to visibility control in C++. If members are defined within the public area, you can access the members from the outside of the class. However, if the members are defined within the private or protected areas, you can not access the members from the outside of the class. In other words, you can control how a class actually works from others.

 

A car can be a good example to explain what a class and object are. Figure 1 shows the formation of the car class. Let suppose we have a car class. Horsepower, price, color, and, manufacturer can be variables in the class. Go, stop, and turn are function in the class. The car goes forward when you press the accelerator pedal and it stops when the brake pedal is pressed. However, you neven what kinds of mechanical algorithms exists behind the scene, unless you study mechanical engineering.

 

Figure 1 Car class
Figure 2 Objects of car class

Figure 2 shows the objects created from the car class. Different variables are assigned to them, thus, the functions show different performance. However, they are from the same data type, just like the size of int = 10 and int = 20 are the same.

 

Code example 1

#include <iostream>
#include <string>

class car
{
private:
	float cost_benefit_factor = 0.2;

public:
	int horse_power;
	int price;
	std::string model;
	std::string manufacturer;
	std::string color;

// constructor by memeber initializer lists
	car(std::string model, std::string color, 
		std::string manufacturer, int horse_power, int price)
		: model(model), color(color), manufacturer(manufacturer), 
		horse_power(horse_power), price(price)
	{}

	void print()
	{
		std::cout << "Car model :" << model<< std::endl;
		std::cout << "Car manufacturer :" << manufacturer<< std::endl;
		std::cout << "Car color :" << color	<< std::endl;
		std::cout << "Car price :" << price	<< std::endl;
		std::cout << "Car benefit :" << price * cost_benefit_factor<< std::endl;
	}
};

int main()
{
	car car1("Focus", "Red", "Ford", 185, 35000);
	car1.print();
	car car2("Prius", "Blue", "Toyota", 150, 23000);
	car2.print();
}

Figure 2 Result of code example 2

Code example 2 is a program that shows how a simple class works in C++. The "car" class is defined with two access specifiers, public and private. The public consists of public information you might easily get from car manufacturer's catalogs, on the other hand, the private has one variable called "cost-benefit factor" which means how much benefit can be made from their prices. 

 

It is not possible to access to the cost-benefit factor from the outside of the class directly. Instead, there is a function called "print" which shows the overall information of each object and the variable "benefit". From "benefit" you can assume the private variable from the outside of the class. You can also modify private variables using a function declared in the public areas as a bridge to the private variables.


2. Access specifiers

There are three access specifiers in C++ to control the visibility of the data inside a class.

 

[1] Public access specifier

 

Public members of a class are fully accessible outside the class.

 

[2] Private access specifier

 

Private members of a class are not accessible outside the class. They are only accessible by means of methods declared under the public access specifier. The members of a C++ class are private by default, whereas the members of a C++ data structure are public by default.

 

[3] Protected access specifier

 

Protected members of a class are not accessible outside the class; however, they become accessible in inherited classes. An inherited class is a class derived from another class, thus you can recycle the entire or certain part of the target class. I am going to talk about "inheritance" later more.

 

 

Figure 3 Code example 2


3. Four characteristics of a class

I showed you what a class and its syntax in C++, but to begin with, the four major underlying principles of OOP must be explained; which are,

 

[1] Encapsulation

[2] Abstraction

[3] Inheritance

[4] Polymorphism

 

Encapsulation is a concept of hiding core information of a class from outside interference under the private/protected access specifier. As a result, the information is protected without being modified. In order to deal with the core data, special methods are usually declared under the public access specifier.

 

Abstraction is a concept that appears due to encapsulation. By means of hiding core data of a class, what really occurs inside the class is protected from outer interference. Radio buttons are good examples of encapsulation and abstraction. Even if you do not know the mechanical algorithm of each button, you can still play, pause, and record radio by simply clicking the buttons. In other words, abstraction is a way of describing a functionality using pre-defined programming data types such as int, float, and char.

 

Inheritance is the mechanism of creating a new class from an existing class. A new class derived from the existing class is referred to as a "derived class" and the existing class is referred to as a "base class". Inheritance is a result of categorical thinking to define abstract concepts in the real world. For instance, cars and trucks are derived classes from vehicle class and books and newspapers are derived classes from paper documents.

 

Polymorphism is a compound word of poly (many) and morphe (form). In computer science, polymorphism means different objects with different data types can be accessed through the same interface. Therefore it is very efficient.

 

It might be a bit complicated for someone who does not have enough computer science knowledge to understand OOP at the moment. I am going to explain more about OOP and classes in detail to help you reach a full understanding of it.