Skip to main content

Introduction to Object Oriented Programming in C++


Introduction to Object Oriented Programming in C++

In this part you will learn:

1. Why using Object Oriented programming approach?
2. What are Classes
3. Why we use classes
4. What are Access specifiers
5. Basic C++ syntax

Why we use Object Oriented programming approach?
We use object oriented programming in C++ because the procedural language( language containing all the functions and variables within the main of the program) cannot cope with very large projects like when we have to use several hundred of functions within one program. Secondly another main problem with procedural language is that there could be expensive software error in it creating trouble by consuming our time and working. On the other hand object oriented approach is the most easiest approach for programming in terms of design, reliability and understanding, helping the programs to have more arranged and efficient programs.

What are Classes?
In object oriented programming approach we basically work with classes. All objects with the same characteristics (data and functions) constitute one class. Each class contains private, public and protected attributes. Depending upon the user’s requirement a class may contain only variables, only function or both. The fundamental idea in class is that it combines both data and functions into a single unit and objects of the classes can be made in the main of the program. The functions present in a class are called member functions.

Why we use classes?
By using classes we can simplify writing, debugging and maintaining the programs. If you want to modify data in an object, we know exactly what functions should we interact with it. A class is a description of a number of similar objects. A class and an object of that class has the same relationship as a data type and a variable. Classes make our code more reliable, easily modifiable and efficient.

What are Access specifiers?
When we write public, protected or private keywords in a class, we basically are dividing the various attributes of our class into access specifiers. Access specifiers specify that to which extent the member functions or the data members of the class can be accessed within the class or from outside the class. Private data members can only be accessed within the class while public data members can be accessed within the class and outside the class as well. Private mode is the default access mode. While protected access specifier provide intermediate level of access between public and private , by allowing the data members to be accessed directly by objects.
Class in C++ can be defined by following syntax:

class ClassName {
access_specifier:
member variables
member functions

access_specifier:
member variables
member functions
};

Basic C++ syntax:
Here we write the first code for C++

  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;
  4. void main()
  5. {
  6. cout<<"My first C++ code!";
  7. }

#include and #include are the header files required for the C++ program. #include is used for input and output in C++ program, while #include is used to display the output on screen. Cout is an object of an iostream class and is used to display output on the screen. Any thing written after cout In this program the output will be “My first C++ code!”.

Output:
output

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.