Dani 4,084 The Queen of DaniWeb Administrator Featured Poster Premium Member

OOP
C++ is an object-oriented programming language. In other words, it contains what are known as objects, such as classes and structures, which allow the easy manipulation of large amounts of data. The easiest way to think of them are as records.

Creating Data Types
When one creates a class, they are creating their own object type. For example, suppose one wants to write a program to manipulate student records. Each student record contains the student's age and phone number, suppose. This would be accomplished by writing a class named student which handles two variables: age and phone. Throughout the program, one would declare a variable of type student just as easily as one would declare a variable of type int. However, each time a student is created, two variables (age and phone) are seemingly attached to it. This is what makes the class student an object. Objects can be created just as variables and encompass large amounts of data. Classes are generally declared prior to any functions in the C++ program. This way, all functions have the option of declaring what are known as instances of the class.

class student

=> public:

=> => int age;

=> => int phone;

If the above class structure was implemented in C++, one would actually be able to create multiple students from within any function by simply writing the data type, student, followed by the variable name. For example, one could declare student John; and student Anna; John's age, for example could be 16 while Anna's age could be 19.

Public and Private Data Members
In addition, all classes contain a public and a private section. (Note that there is also a protected section which will not be discussed thus far). Each section may only contain variable declarations and function prototypes (declarations). Actual C++ code is always written outside of the class. Information in the public section is visible in all areas of your C++ program. Information in the private section is only visible to other functions within the same class.

Scope Identifiers
In C++, functions generally are not written inside of a class definition. Rather, functions are made reference to via a scope identifier (::), and the function body is placed outside of the function. This adds to the idea that class definitions contain declaration statements exclusively.

Working With Class Data
Truly the easiest way to comprehend classes is via an example. Therefore, the following mini-program has been established based on the example above. Recall how the class handles a student's account record consisting of their age and phone number. Note how the first function located in the public section has no data type. It is known as a constructor function and is used to initialize a new instance of the data type, in this case, a new student. The birthday function is to be called on a student's birthday, and will therefore increment the age of the student in question by one. The move function is to be called if a student moves and changes their phone number. The new integer being passed in is the new phone number. Note how these functions are void. They are not passing back a value because all they need to do is change the age and phone variables. Because these variables are within the same class, all functions in the class have global access to them.

class student
{

private:

int age;
int phone;

public:

student(int, int);
void birthday();
void move(int);
int get_age();
int get_phone();

};

Abstraction
A very large part of classes is the concept of abstraction, meaning that programs are not written entirely by one person but rather by a large team of people. In other words, they must be separated into smaller parts which run independently of one another.

For example, it is very possible for a programmer to create a student record without having to have written the student class. All they need to know is that the student class does exist. The public and private data members play a large role with abstraction.

Obviously a person who writes a class doesn't want everyone who uses the class to have access to all data members. In such a case, there is a huge security hole because anyone can just change any values. For example, one could directly alter the age variable to make a student ten years older instantaneously. The writer of the class, however, only wants the age to be incremented one year at a time, by following a strict procedure in the birthday function. For such reasons, the age and phone variables are private. They can be seen only by other functions in the class (both private as well as public functions).

Public functions can be called from other functions within the program. For example, someone can call the birthday function upon a student record. Internally, this birthday function will increment the age behind the scenes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.