Sunteți pe pagina 1din 10

Concepts of OOPs

C++
Object Oriented Programming
Object Oriented Programming is a powerful way to approach the task of programming. Object Oriented Programming encourages you to decompose a problem into its constituent parts. Each component becomes a self-contained object that contains its own instructions and data that relate to that object.

Concepts of OOPs

1. Encapsulation. - Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps them safe from the outside reference and misuse. - When code and data are linked together, an object is created. In other words, an object is the device that supports encapsulation. 2. Inheritance. - Inheritance is the process by which one object can acquire the properties of another. More specifically, an object can inherit a general set of properties to which it can add those features that are specific only to it. - It allows an object to support the concept of hierarchical classification. - E.g. House is part of a general class Building -> Structure -> Man-made. 3. Polymorphism. - Polymorphism (In Greek Poly means many and Morphs means forms). Hence Polymorphism means many forms. - The advantage of polymorphism is that it reduces the complexity by allowing one interface to specify a general

Object Oriented Programming through C++

class of action. It is the compilers job to select the specific action as it applies to each situation. E.g. The functions abs( ), labs( ) and fabs( ) computes and returns the absolute value of an integer, long and float value. However, in C++, which supports polymorphism, each function can be called by same name, such as abs( ). The type of the data used to call the function determines which specific version of the function is actually executed.

cout

It is predefined stream that is automatically linked to the console when a C++ program begins execution. E.g. cout << expression; << is an operator known as insertion operator.

cin

It is predefined stream that is automatically linked to the input when a C++ program begins execution. E.g cin >> variable; >> is an operator known as extraction operator.

To use the C++ I/O operators, the header file iostream.h must be included in the program.

class

The most important feature of C++ is the class. The class is the mechanism that is used to create objects. It basically defines a new type. It determines what an object of that type will look like. Syntax: class class-name { // private variables and functions

Created by: - Bhaumik Patel 2

Object Oriented Programming through C++

public: // public variables and functions } object-list ; The object - list is optional. The variables and functions declared inside a class are said to be members of that class. By default, all variable and functions declared inside a class are private to that class. This means that they are accessible only by the members of that class. To declare public class members, the public keyword is used, followed by a colon. All variables and functions declared after the public specifier are accessible both by the members of the class and by any other part of the program that contains the class.

Object

Object is an instance of a class. We can consider it as a variable of the class type. An object declaration creates a physical entity of the class type. That is an object occupies physical memory space, but the class does not. Each object of a class has its own copy of every variable declared within the class. But the member functions are not copied, instead they are shared by every object.

Access Modifiers

private, public and protected are known as Access Modifiers as they modify the access privileges of the members of a particular class.

Reference

Reference is an implicit pointer that acts like another name for a variable. That is, reference is an alias to an existing vaiable. E.g. int x = 5;

Created by: - Bhaumik Patel 3

Object Oriented Programming through C++

int &y = x; Notice the & in front of y. Here y is a reference to x. Thus any change to y will affect x and vice versa. Reference variables can i. Be passed to a function. ii. A reference can be returned by a function. iii. An independent reference can also be created.

Call by Reference to a function.

E.g. Consider the following function: void swap( int &a, int &b ) { . } When the function is called as show below: int x = 5; int y = 7; swap( x, y ); it exhibits a call by reference as a references x and b references y.

Function Overloading

When two or more functions are having same name but different signature then such functions are said to be overloaded functions. The signature of the functions can differ in three ways: i. Number of parameters. ii. Type of parameters. iii. Sequence of parameters.

Created by: - Bhaumik Patel 4

Object Oriented Programming through C++

E.g. iv. v. vi. vii. viii. ix. x. xi. xii. xiii. xiv. void swap( int, int ); void swap( float, float ); void swap( char, char ); void swap( char *,char *); int abs( int ); float abs( float ); double abs( double ); void set( int ); void set( int, int ); void set( int, char ); void set( char, int );

Default Arguments

Default arguments allow you to give a parameter a default value when no value is specified to that parameter at the time of the function call. E.g. void f ( int a, int b=7 ); Now this function can be called in the following ways: f( 5,7 ); f( 4 ); // here the second argument b is not passed So b is assigned the default value 7.

The default arguments can not be followed by ordinary arguments. i.e. void f ( int a, int b=0, int c ); // not valid void f ( int a, int b=0, int c=2 ); // valid void f ( int a=2, int b=1, int c=4 ); // valid

Inline Functions

Inline functions are not actually called but, rather, are expanded in line, at the point of each call. This is much same as the macros in C. The advantage of in-line functions is that they have no overhead associated with the function call and return

Created by: - Bhaumik Patel 5

Object Oriented Programming through C++

mechanism which means that they can be executed much faster than the normal functions. The disadvantage is that if they are too large and called too often, then the program grows larger and so only short functions are declared inline. E.g. inline int even( int x ){ return !(x%2); } ioid main( ){ if( even(10) ) cout<< 10 is even << endl; } Here the function call if( even(10) ) cout<< 10 is even << endl; is equivalent to if( !(10%2) ) cout<< 10 is even << endl;.

Scope Resolution Operator ( :: )

The scope resolution operator is used to resolve or reveal the scope of a particular member of a class. A class generally contains only the declarations of the member functions. When the functions are to be defined, then they are defined outside the class. But still they should belong to the class that has declared them. This is obtained by the scope resolution operator (:: ). Syntax: <return type> <class name> :: <function name> ( params list ) { }

Constructor

Constructor is member function which i. Has same name as the class and ii. Does not have a return type.

Created by: - Bhaumik Patel 6

Object Oriented Programming through C++

Constructor is automatically called each time an object of the class is created. It is generally used for initializing the member variables. As constructors are also functions, constructor overloading is also possible. E.g. class Student { int rollno; public: Student( ) { rollno = 1; } Student( int a ) { rollno = a; } }; Student s; // This line creates an object and hence invokes the constructor Student s1( 5 ); // Invokes parameterized constructor.

new

new is operator which is used for dynamic memory allocation. Syntax: <data type> pointer_variable = new <data-type> E.g. int *n = new int;

Created by: - Bhaumik Patel 7

Object Oriented Programming through C++

long *arr = new long[ 5 ] ; The case of dynamic object creation is as follows: Student *s; // This line does not invoke the constructor as s is only a pointer which can point to a Student object. s = new Student; // Here the new operator creates an object and hence the constructor is invoked.

delete

delete is an operator which is used to free the occupied memory. Syntax : delete <pointer_variable>; E.g. int *p; void main( ) { p = new int; delete p; }

Whenever the scope of a variable completes the variable gets deleted automatically and the memory occupied by that variable is also cleared. But when the memory allocation is done dynamic as shown in above example, when the scope completes only the pointer gets deleted but the memory location to which the pointer was pointing is not made free automatically. It has to be done explicitly and is done with the use of deleteoperator. E.g. int *p; void main( ) { P = new int[ 5] ; delete [ ] p;

Created by: - Bhaumik Patel 8

Object Oriented Programming through C++

} Here the statement delete [ ] p deletes each element in the array. It does not cause p to be freed multiple times. p is still freed only once.

Destructor

Destructor is a member function which i. Has same name as the class and prefixed by ~ tilde character. ii. Does not have a return type. It is automatically invoked whenever an object is deleted. It is basically used to free the memory occupied the members. E.g.class Array { int *arr; public: Array( ) ~Array( ) }; { { arr = new int[ 7 ]; } delete arr; }

this

this is a pointer which point to the current object. It is automatically passed to the any member function when the function is called. E.g. class Inventory { double cost; public: Inventory( double cost )

Created by: - Bhaumik Patel 9

Object Oriented Programming through C++

{ this->cost = cost; // R.H.S cost variable is parameter variable. // L.H.S cost variable is member variable. } }; void main( ) { Inventory iobj ( 500 ); } Object Oriented Programming through C++

Created by: - Bhaumik Patel 10

S-ar putea să vă placă și