Introduction to Object Oriented Programming in C++

Submitted by: 
Language: 

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<<” is displayed as it is on the screen until “;, getch is used to hold the output on the screen, without it the screen would just flash and console will exit.
In this program the output will be “My first C++ code!”.

Output:
output


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.