Object Oriented Programming through C++

 Object Oriented Programming through C++

Prof. D. V. Patil, Asst.Professor,

SVERI’s College of Engineering,Pandharpur

Department of Computer Science and Engineering

dvpatil@coe.sveri.ac.in

C++ is a general-purpose programming language and widely used nowadays for competitive programming. It has imperative, object-oriented and generic programming features. C++ runs on lots of platform like Windows, Linux, Unix, Mac, etc.However to become proficient in any programming language, one Firstly needs to understand the basics of that language. When we consider a C++ program, it can be defined as a collection of objects that communicate via invoking each other's methods. Following are some of the basic parts of C++:

1.    Object− Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors - wagging, barking, eating etc. An object is an instance of a class.

2.    Class − A class can be defined as a template/blueprint that describes the behaviors/states that object of its type support.

3.    Methods − A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.

4.    Instance Variables−Each object has its unique set of instance variables. An object's state is created by the values assigned to these instance variables.

v  First simple C++ program:

 


Explanation of above program:

Ø  The C++ language defines several headers, which contain information that is either necessary or useful to your program. For this program, the header <iostream> is needed.

Ø  The line using namespace std; tells the compiler to use the std namespace. Namespaces are a relatively recent addition to C++.

Ø  The next line '// main() is where program execution begins.' is a single-line comment available in C++. Single-line comments begin with // and stop at the end of the line.

Ø  The line intmain() is the main function where program execution begins.

Ø  The next line cout<< "Hello World"; causes the message "Hello World" to be displayed on the screen.

Ø  The next line returns 0; terminates main ( ) function and causes it to return the value 0 to the calling process.

v  Compile and Execute C++ Program

1. Let's look at how to save the file, compile and run the program. Please follow the steps given below (for open source platform like linux,unix,dev++ etc.)

1. Open a text editor and add the code as above.

2. Save the file as: hello.cpp

3. Open a command prompt and go to the directory where you saved the file.

3. Type 'g++ hello.cpp' and press enter to compile your code. If there are no errors in your code the command prompt will take you to the next line and would generate a. out executable file.

4. Now, type 'a.out' to run your program.

5. You will be able to see ' Hello World ' printed on the window.

2. If you are using turbo c++ compiler then use following procedure to compile and run program:

1. Open a text editor and add the code as above.

2. Save the file as: hello.cpp

3. For compiling program use Alt+F9

4. If there is no error then run the program using Ctrl+F9

v  Access specifiers in CPP:

u  Access modifiers are used to implement an important feature of Object-Oriented Programming known as Data Hiding

u  Access Modifiers or Access Specifies in a class are used to set the accessibility of the class members.

u  That is, it sets some restrictions on the class members not to get directly accessed by the outside functions.

u  There are 3 types of access modifiers available in C++:

1.                Public

2.                Private

3.                Protected

Let’s see all specifiers in detail:

1. Public:

u  All the class members declared under public will be available to everyone.

u  The data members and member functions declared public can be accessed by other classes too.

u  The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class.

u  Example:

                             Program                                   Output               

 

2. Private:

u  The class members declared as private can be accessed only by the functions inside the class.

u  They are not allowed to be accessed directly by any object or function outside the class.

u  Only the member functions or the friend functions are allowed to acess the private data members of a class.

u  Example:

 

                                       Program                                           Output

3. Protected:

Protected access modifier is similar to that of private access modifiers, the difference is that the class member declared as Protected are inaccessible outside the class but they can be accessed by any subclass (derived class) of that class.

v  Array of Object:

u  An object of class represents a single record in memory, if we want more than one record of class type, we have to create an array of class or object. As we know, an array is a collection of similar type, therefore an array can be a collection of class type.

u  An array of objects is declared in the same way as an array of any built-in data type.

u  The syntax for declaring an array of objects is

                 class_namearray_name [size] ;

 

OUTPUT:


 

v  Constructor

l  Why constructors are required?

u    Initialization of data members in class is not allowed in C++.

u   Data members should be accessed or used through member function of that class only.

u   A constructor in C++ is a special method that is automatically called when an object of a class                      is created.

u   It is special because its name is same as the class name.

u   Constructor is invoked whenever an object of that class is created.

u   It is called as constructor because it constructs the values of data members of the class.

l  Characteristics of Constructor:

u Constructor has same name as the class itself

u Constructors don’t have return type(not even void)

u A constructor is automatically called when an object is created.

u Always written in public only.

 

Three Types of Constructor:

1. Default Constructor

2. Parameterized Constructor

3. Copy Constructor

 

 

Reference:

1. www.google.com

2. Book reference: Object oriented programming through C++- by E.Balgurusammy

Comments