26
1 CS6456 OBJECT ORIENTED PROGRAMMING UNIT - I OVERVIEW Why Object-Oriented Programming in C++ - Native Types and Statements Functions and Pointers- Implementing ADTs in the Base Language. Overview of Object Oriented Programming. There are 2 ways of problem solving approaches in programming. They are the Procedural Programming Object Oriented Programming Procedural Programming In procedure oriented programming the problem is viewed as a Sequence of things to be done. The main functionality is focusing on functions need for OOPS. OOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming. Program Procedure Object 1 Emphasis is on code Emphasis is on data & functions 2 Larger problem is divided into small program functions Program is divided into object 3 Share global data Data is hidden 4 Data move freely from functions to functions Object communicate through functions Basic concepts of OOPS Objects Classes Data Encapsulation Inheritance Polymorphism Dynamic binding Data abstraction Message passing

CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

1

CS6456 OBJECT ORIENTED PROGRAMMING

UNIT - I OVERVIEW

Why Object-Oriented Programming in C++ - Native Types and Statements –Functions and

Pointers- Implementing ADTs in the Base Language.

Overview of Object Oriented Programming.

There are 2 ways of problem solving approaches in programming. They are the

Procedural Programming

Object Oriented Programming

Procedural Programming

In procedure oriented programming the problem is viewed as a Sequence of things to be

done. The main functionality is focusing on functions need for OOPS.

OOPS is an approach to program organization and development that attempts to eliminate

the pitfalls of conventional procedure oriented programming.

Program

Procedure Object

1 Emphasis is on code Emphasis is on data & functions

2 Larger problem is divided into

small program functions Program is divided into object

3 Share global data Data is hidden

4 Data move freely from functions

to functions

Object communicate through

functions

Basic concepts of OOPS

Objects

Classes

Data Encapsulation

Inheritance

Polymorphism

Dynamic binding

Data abstraction

Message passing

Page 2: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

2

Objects

Instance of a class which are called as the basic run-time entities

The class variables are called as objects may be a person, place or any item.

The syntax for declaring object is

Classes

A class is a collection of objects in which the data and functions are put together

The syntax of a class is

The class name must be preceded by the key word class

Inside the class, there are two keywords used private and public public which are

called as access specifiers.

Rules for declaring class name

The class name must begin with the letters, it may be followed by letters or digits or

underscore.

The name of the class must not be same as the keyword or reserved word.

It should not contain any special character such as ~ ! @ # $ % ^ & * etc.,

Encapsulation

It means binding of data and method together in a single entity called class.

The data inside a class is accessible by the function

in the same class. It is normally not accessible from

the outside of the component

Class name Object name

Fruit Mango

Class Class name { Private: Variable declaration; function declaration; public: declaration; };

Data

Method

method

method

Page 3: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

3

Inheritance

It is the property by which new classes are created using the old classes (or)

The process by which objects of one class acquire the properties of objects of another

class

Robin, Swallow Penguin, Kiwi

Polymorphism

It is the ability to take more than one form

Overloading comes under the concept of polymorphism

Reusability

It means making use of existing class by adding the additional features and

capabilities within it.

Dynamic Binding

Binding refers to the linking of a procedure call to the code to be executed in response

to the call

Dynamic Binding is the run-time determination of which function to call for a

particular object based on the type of argument.

Data Abstraction

Wrapping up of data and functions into a single unit is called as encapsulation.

Insulation of data from direct access by the program is called data hiding or

information hiding.

Abstraction refers to the act of representing essential features without background

details.

Student obj;

Box

Draw

( )

Triangle

Draw

( )

Shape

Draw

Circle

Draw

( )

Birds

attribute

Feature Lay eggs

Non-flying Birds

attribute

Flying Birds

attribute

Page 4: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

4

{

Obj. input ( ); abstract data

Obj. output ( );

Benefits of OOPS

Existing classes are eliminated using inheritance.

Build programs by communicating with each other.

It is possible to create multiple objects for a given class.

Partitioning the code is easy.

Used in upgrading the system.

Disadvantages

Complex to implement

Members declared as private are not accessible by others

Everything must be arranged in the forms of classes and modules.

Application of OOPS

Business logic applications

Real time simulation

Knowledge based & expert

Web based applications

Simulation and modeling

CAD/CAM Systems

Games programming

A.T system

Neural networks

Sample program

# include <iostream.h>

class student void student: :get data ( )

{ {

char name [30]; cout << “Enter your name:”;

int roll no; cout << “ur num”;

public: cin >> name;

void get data ( ); cin >> roll no;

void display ( ); }

} main ( )

student s;

Data Encapsulation Data Abstraction

Process of binding data members of a class Process of eliminating unimportant details of a class

Depends upon object data type Independent upon object data type

Used in S/w implementation process Software design process

Achieved by inheritance Represented by using abstract classes

Page 5: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

5

s.get data ( );

geten ( );

}

Native types and statements.

C + + Program elements

Every C++ program contains the logical units called tokens. Tokens are basically

collection of characters that belongs to some category. The tokens in C++ can be

Keywords

Identifiers

Literals

Operators

Comments

Comments are those in a program which are non executable. They simply provide the

information about the programming statements.

/*……… */ Single line comment

// multiline comment

Keywords

Keywords are special reserved words associated with some meaning. The keywords are

asm private try virtual

auto protected throw inline

bool public typeid default

double operator namespace

new else this etc.,

Identifiers

The identifier is a sequence of one or more letters or alpha numeric characters.

Rules for identifiers:

Identifiers should not start with digit

Special characters except underscore ware allowed.

Should not have any blank space with identifier name.

Should not be a keyword

Should be meaningful.

Literals

Literals are the constant values. Constants are used to define the fixed values in C++.

100 Decimal number

78.66 Real number with decimal point

“Hello” String constant

“T” Character constant.

Page 6: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

6

Escape Sequence

Escape sequences are special purpose non printing characters

\\ backslash

\b backspace

\” Double quotes

\n Newline

\t Tab

\’ Single quote

\o Null character

Operators

a+b is an expression in which the “+” operator to perform operations

While using the operators to perform operations it follows some hierarchy of

operations

Priority Operators

1 , / , %

2 + , -

3 =

enum

To create user defined type names the keyword enum is used. the syntax of using enum is

enum name { list of names } variables;

#include <iostream.h>

void main ( )

{

Enum mylist {small, middle = 5, large} a,b;

a=middle;

b= large;

count <<” a= “<<” b;

}

Output: a = 5 b = 6

Program Structure

There are four sections in the structure of C++ Program.

Include file section

Class declaration section

Function definition section

Page 7: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

7

#include<iostream.h>

Void main ( )

{

cout << “Hello world”;

}

Output: Hello world

Input and output

The input operation is called extraction because data is extracted from keyboard and the

operator >> is called extractor.

cin >> a

The output operation is called insertion because data is inserted. The operator is called as

insertion operator.

cout << a;

Data types

Variables

A variable must be defined before using it in a program. It reserves the memory required

for data storage and associates with a symbolic name.

Syntax

datatype vername / … varname 2;

Initialization of variables

C++ permits initialization of variables at runtime. This is known as dynamic

initialization. The declaration and initialization can be done simultaneously at the place where

the variables are used for the 1st time.

datatype var-name = constant value;

datatype var-name (constant value)

(eg) int a=10;

#include <iostream.h>

Main function definition

Structure

Union

Class

Enumerator

Array

Function

Points

Reference

User defined

C++ data types

Built-in Derived

Integer

type

int char

Void

type

Float

type

float double

Page 8: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

8

void main ( )

{

int a,b;

int c = 50;

float d;

a = c;

b = c+50;

d = 50.10;

count<< “d=” <<a<< “\n”;

count << “b=” << b << “\n”;

count << “c=” << c << “\n”;

count << “d=” <<d;

}

Output: a= 50 b = 100

c= 50 d = 50.10

Reference variable

A reference variable provides an alias for the previous defined variable

Syntax

datatype & reference name = variable name.

int & a = b;

A reference variable must be initialized at the time of declaration. This establishes

correspondence between the reference and the data object which it names.

Type conversion

The type conversion is a process in which variable in one data type can be converted to

another data type.

Some data types can be automatically converted by the compiler. This automatic

conversion is called as implicit conversion.

# include <iostreem.h>

void main ( )

{

jnt x;

flot y = 99.99

x=y; here value of x becomes 99 and .99 will be truncated.

Control structures

The control structures are the set of statements that are used to control the flow of

execution of a program. The control statements include the

If statement While statement

Do-while statement Switch-case statement.

If statement

The if statement is of two types

(i) Simple if

(ii) Compound if

Simple if statement

The if statement in which the only one statement is followed by that if statement

is followed by that if statement

int & x = y [100]

Page 9: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

9

Syntax

if (apply condition)

execute the statement if condition is true.

Example :- if (a > b)

cout << “\n a is Big”;

Compound if statement

If there are more than one statement those have to be executed when the if

condition is true. All are placed inside curly brackets.

Syntax

If (condition is true)

{

Execute this statement

}

If … else – if ( condition) execute this statement, else execute this statement.

if (a > b)

count << “\n” The a is big”;

else

count << “b Big brother”;

Sample Program

if (raining = = True)

{

cout<<“\n I won’t go out”;

cout<<“\n I will watch TV Serial”;

count<<“\n Also will enjoy Coffee”;

}

else

{

cout<<“\n I will go out”;

cout<<“\n And will meet my friend”;

count<<“\n We will go to a movie”;

}

If else if statement

The syntax of the if…..else if statement is

if (condition is true ?)

execute this statement

else if (is this another condition true?)

execute this statement

else

this is the final choice.

Example

if(age == 1)

Page 10: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

10

cout<<”you are an infant”;

else if (age = = 20)

cout<<”you are grown up”;

else

cout <<”you are an adult”;

Program for fixing the Bonus of employees using nested if else if.

# include <iostream.h>

Void main ( )

{

Long int basic;

Float bonus gross;

cout<<”\n Enter the basic salary”;

cin >> basic;

if(basic<10000)

{

bonus =0.75*basic;

gross = basic + bonus;

cout<<”\n Your salary is =”<gross;

}

else if ( basic >10000 && basic<=20000)

{

bonus = 0.50 * basic;

gross = basic + bonus;

cout<<”Your Salary is “<gross;

}

else

{

bonus = 0.0;

gross = basic + bonus;

cout<<’\n No Bonus and your salary is =”<<gross;

}

}

Output

Enter basic salary 55000

No Bonus and your salary is = 55000

Enter basic salary 7500

Your Salary is =

Switch Case statement.

You can compare the switch statement with the menu-card in the hotel. You select the

menu and that only will be served to you.

If - else statement. if (Idli Sambar is available)

“Eating Idli Sambar”;

else if ( Dosa is available)

“Eating Dosa”;

else

“A Cup of Coffee”;

Where as switch (my-choice)

Page 11: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

11

{

case Idli-sambar: “Eating Idli-Sambar”;

break;

case Dosa: “ Eating Dosa”;

break;

default: “Drinking Coffee”;

break.

Do-while Statement

The other form of the control statement is do ….while statement.

do

{

Execute the statement;

that’s all;

} while (some condition);

Example

count = 1;

Do

{

cout<<”\n I am on the 1st line of do-while”;

cout<<”\n I am on the 2nd

line of do-while”;

count ++;

} while (count <=5);

While statement

The while statement is exactly similar to the do ….. while statement. But the only

difference between the two is that in the do …. while body has to be executed at least once.

while ( condition is to be checked )

{

execute this statement;

}

Example

count = 1;

while ( cout < = 5)

{

cout<<’\n I am on 1st line of while”;

cout<<’\n Last line of while”;

count ++;

}

For Loop

Page 12: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

12

The interesting thing in control structure is Looping. Loop can also be categorized into

Simple loop and Compound loop.

Simple for loop

for (statement 1; statement 2; statement 3)

execute this statement;

Compound for loop

for (statement 1; statement 2; statement 3)

{

execute this statement;

}

Example Program

#include<iostream.h>

voidmain ( )

{

int count;

for (count =1; count < = 5; count = count +1)

{

cout<<”\n Testing the for loop, Tested OK!”;

cout<<”\n Printing the statement for “<<count<<”time”;

}

}

Output

Testing the for 100p, Tested OK!

Printing the statement for 1 time , , , , , , , , , ,

Testing the for 100p, Tested OK! Printing the statement for 5 time

Write a program to print the following output using for loops.:

1 , 22 , 333 , 444

Program

#include<iostream.h>

voidmain ( )

{

int count;

for ( int i = 1; i < = 4; i ++)

{

count = i;

for( int j = 1; j <=count; j ++)

cout << count;

cout << end e;

Page 13: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

13

}

}

Functions and pointers

A C++ function has the following methods-

1. Definition of function

2. Call to the function

Definition of the function

The syntax for the function definition is

Data - type – name ( data – type parameter, ……)

{

Body for logic

}

A function prototype is a declaration of function without specifying the

function body. i.e., the function prototype specifies name of function, argument

type and return type.

The call to a function

The call to the function is given by simply by giving the name of the function.

The syntax for this is

Name of function ( parameter 1, parameter 2, ……… , parameter n);

There are various types of the function.

1. Passing nothing and returning nothing

2. Passing the parameters and returning nothing

3. Passing parameters and returning something.

1. Passing nothing and returning nothing

#include <iostream.h>

void sum ( )

{

int a,b,c;

cout<<”\n Enter the 2 numbers:”;

cin>>a;

cin>>b;

c=a+b;

cout <<’The addition of 2 numbers is “<<c;

}

void main ( )

Page 14: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

14

{

Sum ( ) /* call to the function */

}

Output

Enter the 2 numbers : 2 3

addition of 2 numbers : 5

Here no parameter is passed and no function is returned .

(e.g.)- void main ( void ) can be written instead of main ( )

2. Passing the parameter and returning nothing

#include <iostream.h>

void sum ( int x, int y) /* definition */

{

int c;

c = x + y;

cout <<” The addition is :”<<c;

}

void main ( )

{

int a,b;

cout <<”Enter 2 numbers”;

cin >>a;

cin>>b;

} Sum ( a,b); /* call */

Output

Enter the 2 numbers: 2 7

Addition is 12

There are actually two methods of parameter passing.

Call by value:

If the parameters are called by value, it is called as call by value.

Sum ( a,b);

Call by reference:

If the values of the parameters are caused by reference it is called as call by

reference.

Sum (&a, &b) void sum (int*x, int *y)

Same program but c = *x+*y Sum (&a, &b)

3. Passing the parameter and returning from the function

In this the parameters are passed to the function and the function also returns

something.

Page 15: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

15

#include <iostream.h>

Int sum ( int a, int b)

{

int c= a+b;

return c;

void main ( )

{

int a, b, c;

int sum (int, int);

cout <<”Enter 2 numbers”;

cin>> a;

cin>> b;

c = sum (a+b);

count <<”\n The addition is =”<<c;

}

}

Output

Enter the 2 numbers: 4 5

Addition is 9

Default Arguments:

Default argument is an argument to the function to which the default value is provided. If

the user does not supply a value to this argument then the default value is provided. But if the

user provides some different value to this argument then this new value is assigned to the default

parameter.

# include<iostream.h>

Class test

{

private

int x,y;

public:

void fun (int x, int y = 100)

{

cout <<’x=’’<< x << end l;

cout <<’y=’’<< y << end l;

}

};

test obj;

cout<<” Displaying default argumentvalue”<<end l;

obj. fun (10);

cout<<” overriding the default argument”<<end l;

obj. fun (10, 20);

//returning c which is of int type // so data

type of sum is int.

Page 16: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

16

}

Output

Displaying the default argument value

argument value

x = 10

y = 100

overriding the default argument

x = 10

y = 20

Inline functions

The inline function is a function whose code is copied in place of each function call. The

inline specifies that the compiler should insert the complete body of function in every context

where the function is used.

The fact is that if the function definition contains very few lines of code then making the

function inline is always preferred. Otherwise it is not at all preferred to make the function inline

because large code gets copied many times and large amount of space will be wasted by storage

of same code.

# include<iostream.h>

inline int sum ( int x, int y)

{

return (x + y );

}

void main ( )

{

int a, b ;

a = 10 ;

b = 20 ;

` cout << “ The sum is “<<sum(a , b );

}

Output

The sum is 30

Situations in which inline function may not work:

1. For the function that contain Static Variables.

2. For the functions returning values if-for loop, switch or goto exists.

3. For the functions not returning a function and if return statement exists

4. If the inline functions are recursive function.

In line function Macro

Inline functions are analyzed by the Macros are expanded by C++

Page 17: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

17

Scope of variable

There are two types of scope of the variables.

Local Scope

File Scope

Local Scope

The local scope is limited to a particular block of code. The variables declared

within some function passes the local scope.

File scope:

The variable is might be declared in such a way that its scope is the entire file.

Storage classes

The storage classes are used to define the scope and life time of the variables and

functions. There are 4 types of storage classes. They are

auto

default for a function or a variable

storage is automatically allocated and freed.

freed

auto can be used only within functions.

auto int a is the same ass int a

register

used to define local variable s

should be used only for variables that requires quick access

register int count;

static

the static is the default storage class for global variables.

The static can also be defined within a function. If it is defined within the function

then this variable lasts for the life of the program

static int count;

compiler preprocessor

Inline functions perform type checking

and arguments

Macros do not perform type

checking and donot check

arguments

Page 18: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

18

extern

If a variable is declared in one file but referenced in another then extern keyword

is used to inform the complier of the variable’s existence.

An extern declaration does not create any storage.

extern int a;

Name spaces

Name spaces are used to group the entities like class, variables, objects, function under a

name. the name spaces help to divide global scope into sub-scopes, where each sub-scopes has

its own name.

#include <iostream.h>

Using name space std;

namespace ns1;

{

int a = 5;

}

namespace ns2;

{

char a [ ]= “Hello”;

}

int main ( )

{

cout <<ns1::a <<end l;

cout <<ns2::a <<end l;

return 0;

}

Output 5 Hello

In the above program there are 2 different name spaces declaring the variable a. The

variable a in the first namespace ns1 of the type int but the variable a in the namespace ns2 is of

array of characters. There is no re-declaration error for variable a.

Keyword using

The keyword using is used to introduce the namespace being used correctly.

using namespace ns2.

Std. namespace

It is used in the program which uses any entity declared in iso stream.

Rules of namespace

Following are the rules of namespaces.

1. If we do not specify using namespace std for the std namespace then for each cout

statement we have to write std :: cout

2. The code which is not explicitly declared within the namespace is considered to

be in global namespace.

Page 19: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

19

3. Namespace resolution is hierarchical

4. Cannot have more than one meaning

5. An identifier defined in a namespace is associated only with that namespace.

Pointers

Pointer is a derived data type that refers to another data variable by storing the variables

memory address rather than data. The declaration of pointer variable takes the following form.

datatype *pointer-variable;

int *ptr;

int * ptr, a; //declaration

ptr = &a; //initialization

ptr containes address of the vaiable

& address of operator

* used to retrieve the address.

Manipulation of pointers

* Pointer – variable;

dereferencing operator

#include <iostream.h>

void main ( )

{

int a =10 , *ptr;

ptr = &a;

cout <<”value of a is”;

*ptr = (*ptr)/2;

cout<<”value of a is “<<(ptr);

}

Pointer Types

There are various pointer type declarations. For example – if we declare the integer

pointer variable as

int*ptr then

Reference

To assign the reference variable to the pointer variable we can declare it as

ptr = &marks;//suppose marks is a variable whose address can be assigned to the ptr.

Null

The special value O can be assigned to the pointer variable. Sometimes it denotes

the end of the data structure such as linked list. It can be declared as

Ptr=0;

Type casting

For converting the data type the typecasting is done.

Ptr = static <int*>(1255)

The absolute memory address 1255 is used in this type casting.

Page 20: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

20

Use of void pointer

Can be used to point to the objects of any data type. It is also known as generic

pointer.

Void pointer is declared like normal pointer declaration, using the keyword void

Void *ptr;

Arrays and Pointers

#include <iostream. h>

#include <conio.h>

#include <stdlib.h>

void main ( )

{

int x[10], I;

clrscr ( )

cout <<”\n Enter the numbers”;

for (i=0; i<5; I ++)

cin>> x [ i ]

cout<<”you have entered”;

for (i=0; i<5; I ++)

cout<<”in At address”<<( x + 1)

<<”the value is “ <<*( x + 1 );

getch ( );

}

Output

Enter the 5 numbers 11, 22, 33, 44, 55

You have entered at address

Pointers and strings

Char num [ ] = “one”;

Const char * num ptr = “one”;

Example

Output

TTTT sssttt test

tset reverse string

Pointer to function

The pointer to function is known as call back function.

datatype (*function-name);

#include <iostrem.h>

Void main ( )

{

char str { } = “Test”;

int len = strlen (str);

for (int I = 0; i<len: i++)

{

cout<<str [i] <<i[str]<<*cstr + i) << * ( i-str);

}

cout<<end l;

Int len 1 = len/2;

len - -;

for (i=0; i<1; i ++)

{

str [i] = str [i] + str [len – i]

str [len-i] = str [i]-str [len-i];

str [i] = str[i]-str[len-i]

cout<<’string reverses”;

}

Page 21: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

21

Output

1 + 2 = 3

3 – 2 = 1

Pointers to object

class name * ptr – to - object;

ptr – to - object = & object;

ptr – to - object = new class name

Delete the pointer

class name * ptr – to - object;

ptr – to - object = & object;

delete ptr – to - object

This Pointer

Every member function of a class is bound with a pointer called this which points to the

object with the member function is associated

It is a pointer that points t oitself.

#include <iostrem.h>

Typedef void (*funptr)(int,int);

void add (int i, int j)

{

cout<< I <<”+”<<j<<”=”,,i+j;

}

void subtract (int i, int j)

{

cout<<i<<”-“<<j<<”=”<<i-j;

}

void main ( )

{

fun ptr, ptr;

ptr = &add; ptr(1,2);

cout<<end l;

ptr = & subtract;

ptr (3,2);

}

#include <iostrem.h>

class Sample

{

Public;

int a,b;

Sample ( )

{

a = 10, b = 20;

}

};

void main ( )

{

sample * ptr;

sample S;

ptr = &S;

s. display ( );

ptr display ( );

(or)

* ptr. display ( );

}

Page 22: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

22

Output

address of my object

this is set data: 20

This in show data 20.

New and Delete operators:

The dynamic memory allocation is done using an operator “new”. The syntax of dynamic

memory allocation new is

new data type;

int*p;

p = new int;

(or)

int * p

p = new int [5]

delete variable – name;

delete p;

Implementing Abstract Data Types

Abstract data type is a collection of instances and corresponding operations on it. In ADT

all the implementation details are hidden, what is to be mentioned bt ADT

The abstract data types consist of the following things.

Data used along with the data types

Declaration of functions which specify only the purpose

Behavior of function can be specified with the help of data and functions together.

Thus ADT allows programmer to hide the implementation details. Hence it is called as

abstract.

/* ADT for a Set of integers.

Abstract Data Type Set

#include <iostrem.h>

class Test

{

Private;

int a;

Public

void set data (int x)

{

a = x ;

cout<<”address of my object;

this in setdata 9 ) “<<this;

this a = x;

}

void show data ( )

{

cout << a;

cout <<”this in show data ( )”;

cout << this a;

}

};

void main ( )

{

Test my;

my.set data (20);

my.show data ( );

}

Page 23: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

23

{

Instances: set is a collection of integer type of elements.

Preconditions : none

Operations:

1. Store ( ): used for storing integer element

2. Retrieve ( ): retrieve desired element

3. Display ( ):

}

Class of ADT

class Student

{

private

int roll;

char name [10];

public

void get-data ( ) ;

};

The above class represents the abstract Structure as the implementation details are hidden

from the outside world.

Implementation of stack ADT

Stack is a data structure in which the elements can be inserted ordeleted from only one

end called top. The data structure is also called as LIFO because the element inserted in the stack

lastly is removed first.

With reference to stack – the insertion operation is called push and the removal operation

is called POP.

Push Operation

Pushing 10, 20, 30

Pop Operation

Stack

40

30

20

10

Top

10

Top

20

10

Top

30

20

10

Top

Empty

Stack

10

30

20

10

20

10

Page 24: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

24

Abstract Data Type Stack

{

Instances:

Stack is a collection of elements in which insertion and deletion of elements is

done by one end called top.

Operations:

1. Push: By this operation one can push elements onto the stack. Before performing

Push we should check whether the stock is full or not.

2. Pop: By this operation one can remove elements onto the stack. We should check

whether the stock is empty or not.

Pre-condition:

1. Before pushing the element onto the stack we must check wether stack is full or

not.

2. Before popping, have to check whether empty or not

}

*****

PART A

Why Object Oriented Programming

1. What is object oriented programming? (MAY/JUNE 2016) (NOV/DEC 2016)

2. Define data abstraction. (MAY/JUNE 2016) (NOV/DEC 2016)

3. Distinguish between objects and classes (MAY/JUNE 2016)

4. Write any two striking features of object oriented programming. (NOV/DEC 2015)

5. What are the advantages of object oriented programming over structured oriented programming?

(APR/MAY 2015)

6. With respect to C++ distinguish objects and classes. (NOV/DEC 2010) (NOV/DEC 2012)

(NOV/DEC 2013)

7. What is encapsulation? Do friend functions violate encapsulation? (NOV/DEC 2010)

8. Define abstraction and encapsulation. (APR/MAY 2011) (NOV/DEC 2013) (MAY /JUN 2014)

9. What is object oriented programming and What is the need for object oriented programming?

(NOV/DEC 2013)

10. What are the merits of using classes ? (MAY /JUN 2014)

11. What are the rules for declaring class name?

12. Define dynamic binding

13. Distinguish data abstraction and Data encapsulation

Functions And Pointers

14. Differentiate a constant pointer and a pointer to a constant with an example. (NOV/DEC 2015)

15. Illustrate the usage of this pointer in C++. (NOV/DEC 2015)

16. What are the advantages of an inline function? (APR/MAY 2015) (NOV/DEC 2016)

17. What is the difference between a pointer and a reference ? (NOV/DEC 2011)

18. Write the difference between call by reference and call by value

19. What is an default argument?

20. Define inline functions

Page 25: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

25

Native Types And Statements- Implementing ADTs In Base Language

21. Define attribute . (MAY /JUN 2013)

22. What is a literal?

23. Define escape sequence

24. What is hierarchy of operators?

25. How dynamic memory allocation is performed in OOPS?

PART B

WHY OBJECT ORIENTED PROGRAMMING :

1. Explain the major features of object oriented programming with illustrations and neat diagram (16 M)

(APR/MAY 2015) (NOV/DEC 2010) (Or) Briefly describe the object oriented features supported

by C++. (4M) (NOV/DEC 2015) (Or) Explain the basic object oriented concepts (10M)

(NOV/DEC 2011) (Or) Explain the characteristics of OOPS in detail. (8M) (NOV/DEC 2012)

(NOV/DEC 2016) (or) List out the features of object oriented programming.(5M) (NOV/DEC

2013)

2. Write a C++ program to implement a Binary search Procedure to find whether the given element is

present in the array or not using objects and classes. (6M) (NOV/DEC 2015)

3. List out the differences between procedure oriented programming and object oriented

programming.(8M) (MAY/JUNE 2016) (NOV/DEC 2015) (NOV/DEC 2016)

4. Write a C++ program that will ask for a temperature in Fahrenheit and display it.(6M) (NOV/DEC

2011)

5. Write a C++ program to check if the substring is present in the given string. (8M)(NOV/DEC 2015)

6. Write in detail about the class ,objects, methods and messages(8M) (APR/MAY 2011)

7. List out the application of OOPS.(7) (NOV/DEC 2016)

8. What is dynamic Binding ? How is it achieved? (8M) (NOV/DEC 2011)

9. Distinguish between abstraction and encapsulation.(3M) (NOV/DEC 2013)

10. What are the needs of object oriented paradigm. (8M)(MAY/JUN 2014)

11. Explain the following concepts of Object Oriented Programming with an example.

i) Data abstraction

ii) Inheritance (16 M) (MAY/JUN 2014)

12. Explain the structure of a C++ program.(8M)

NATIVE TYPES AND STATEMENTS:

13. Write short notes on casting primitive data types to object type and vice verse with a an example for

each. (6M) (NOV/DEC 2015). (OR) With the help of a C++ program explain the type conversion

method . (8M)

14. Explain do-while with an example. (8M) (NOV/DEC 2013)

15. What is a namespace? How do you resolve the name conflicts using namespaces? Explain with an

example. (4M) (NOV/DEC 2015)

16. Explain the various operators available in C++ with neat illustration of it. (16 M) (APR/MAY 2015)

17. Explain the various control statements used in C++.((16 M)

FUNCTIONS AND POINTERS

18. Explain how objects are passed as arguments to a function and returned from a function with suitable

examples.(8M) (NOV/DEC 2015)

19. Explain in detail about friend functions in C++ with example. (6M) (MAY/JUN 2013) / (8)

(NOV/DEC 2013) (or)

Page 26: CS6456 OBJECT ORIENTED PROGRAMMINGOOPS is an approach to program organization and development that attempts to eliminate the pitfalls of conventional procedure oriented programming

26

Explain the friend functions and its advantages and disadvantages with an example.(4) (NOV/DEC

2015)

20. Define a class student with related specifications. Use get_input and show_input methods to show the

operations.(8M) (NOV/DEC 2015)

21. Explain about pointers in detail. (8) (MAY/JUNE 2016)

22. Write a C++ program to find maximum of two numbers using inline functions. (4M) (NOV/DEC

2015)

23. Write a C++ program to find the area of square, rectangle, circle using function overloading(8M)

(NOV/DEC 2015)

24. Write a c++program to list out prime numbers between the given two limits.(8) (MAY/JUNE 2016)

(NOV/DEC 2016)

25. Explain function overloading in C++ with an example.(8) (NOV/DEC 2016)

26. Illustrate the concept of function overloading to find the maximum of two numbers.(10M)

(NOV/DEC 2011)

27. What are inline functions? Write a simple code to explain. (8M) (NOV/DEC 2011)

28. Distinguish call by value and call by reference. (8M)

IMPLEMENTING ADTS IN BASE LANGUAGE:

29. Explain dynamic memory management using new and delete operators and write a c++ program for

implementing dynamic memory allocation (8M)(NOV/DEC 2012)

*****