[go: up one dir, main page]

0% found this document useful (0 votes)
25 views142 pages

18BCS33C U3

Uploaded by

rajkumar184
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views142 pages

18BCS33C U3

Uploaded by

rajkumar184
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 142

Government Arts College(Autonomous)

Coimbatore – 641018
Re-Accredited with ‘A’ grade by NAAC

Object Oriented Programmimg with C++

Dr. S. Chitra
Associate Professor
Post Graduate & Research Department of Computer Science
Government Arts College(Autonomous)
Coimbatore – 641 018.
Year Subject Title Sem. Sub Code

2018 -19
OBJECT ORIENTED PROGRAMMING WITH C++ III 18BCS33C
Onwards

Objective:
•Learn the fundamentals of input and output using the C++ library
•Design a class that serves as a program module or package.
•Understand and demonstrate the concepts of Functions, Constructor and inheritance.

UNIT – I
Principles of Object Oriented Programming: Software Crisis - Software Evolution - Procedure
Oriented Programming - Object Oriented Programming Paradigm - Basic concepts and benefits of
OOP - Object Oriented Languages - Structure of C++ Program - Tokens, Keywords, Identifiers,
Constants, Basic data type, User-defined Data type, Derived Data type – Symbolic Constants –
Declaration of Variables – Dynamic Initialization - Reference Variable – Operators in C++ - Scope
resolution operator – Memory management Operators – Manipulators – Type Cast operators –
Expressions and their types – Conversions – Operator Precedence - Control Structures
UNIT – II

Functions in C++: Function Prototyping - Call by reference - Return by reference - Inline functions -
Default, const arguments - Function Overloading – Classes and Objects - Member functions - Nesting
of member functions - Private member functions - Memory Allocation for Objects - Static Data
Members - Static Member functions - Array of Objects - Objects as function arguments - Returning
objects - friend functions – Const Member functions .

UNIT – III

Constructors: Parameterized Constructors - Multiple Constructors in a class - Constructors with


default arguments - Dynamic initialization of objects - Copy and Dynamic Constructors - Destructors
- Operator Overloading - Overloading unary and binary operators – Overloading Using Friend
functions – manipulation of Strings using Operators.
UNIT – IV

Inheritance: Defining derived classes - Single Inheritance - Making a private member inheritable – Multilevel,
Multiple inheritance - Hierarchical inheritance - Hybrid inheritance - Virtual base classes - Abstract classes -
Constructors in derived classes - Member classes - Nesting of classes.

UNIT – V

Pointers, Virtual Functions and Polymorphism: Pointer to objects – this pointer- Pointer to derived Class -
Virtual functions – Pure Virtual Functions – C++ Streams –Unformatted I/O- Formated Console I/O – Opening
and Closing File – File modes - File pointers and their manipulations – Sequential I/O – updating a file :Random
access –Error Handling during File operations – Command line Arguments.

TEXT BOOKS

1.E. Balagurusamy, “Object Oriented Programming with C++”, Fourth edition, TMH, 2008.
Constructor

*A constructor is a special member function whose task is to initialize the objects of


its class.
*It is special because its name is the same as the class name.
*The constructor is invoked when ever an object of its associated class is created.
*It is called constructor because it construct the values of data members of the class.
A :: A( )
output:
object1
m=0
n=100
object2
m=25
n=75
new
delete
delete
Constructors with Default Arguments

Like other functions in C++, Constructors can also have default


arguments
OPERATOR OVERLOADING:-
* Operator overloading is giving additional definition to the C++ opearators.
* All the C++ operators can be overloaded except the following:

1. Class members access operator (. , .*)


2. Scope resolution operator (: :)
3. Size operator(sizeof( ))
4. Conditional operator (? :)

Although the semantics of an operator can be extended, we can't change its


syntax, the grammatical rules that govern its use such as the number of
operands, precedence and associativty.
For example the multiplication operator will enjoy higher precedence than the
addition operator.
When an operator is overloaded, its original meaning is not lost.
For example, the operator +, which has been overloaded to add two vectors,
can still be used to add two integers.
DEFINING OPERATOR OVERLOADING:

• To define an additional task to an operator, we must specify what it means in relation to


the class to which the operator is applied .

• This is done with the help of a special function called operator function, which describes
the task.
• operator functions must be either member function, or friend function.
• A basic difference between them is that a friend function will have only one
argument for unary operators and two for binary operators.
• This is because the object used to invoke the member function is passed
implicitly and therefore is available for the member functions.
• Arguments may be either by value or by reference.

The process of overloading involves the following steps:-

1. Create a class that defines the data type that is used in the overloading operation.

2. Declare the operator function operator op() in the public part of the class

3. It may be either a member function or friend function.

4. Define the operator function to implement the required operations.


Unary Operator Overloading
Unary – operator overloading(using member function):
class abc
{
int m,n;
public:
abc()
{
m=8;
n=9;
}
void show()
{
cout<<m<<n;
}
void operator -- ()
{
--m;
--n;
}
};
void main()
{
abc x;
x.show();
--x;
x.show();
}
Example 2
Binary operator - for subtracting two complex numbers (using friend function)
class complex
{
float real,img;
public:
complex()
{
real=0;
img=0;
}
complex(float r,float i)
{
real=r;
img=i;
}
void show()
{
cout<<real<<”-i”<<img;
}
friend complex operator-(complex p,complex q);
};
complex operator-(complex p,complex q)
{
complex w;
w.real=p.real-q.real;
w.img=p.img-q.img;
return w;
}
};

void main()
{
complex s(3,4);complex t(4,5);
complex m;
m=operator-(s,t);
s.show();t.show();
m.show();
}
Overloading an operator does not change its basic meaning. For example assume the + operator can be overloaded to
subtract two objects. But the code becomes unreachable.

class integer
{
int x, y;
public:
int operator + ( ) ;
}
int integer: : operator + ( )
{
return (x-y) ;
}

Unary operators, overloaded by means of a member function, take no explicit argument and return no explicit values. But,
those overloaded by means of a friend function take one argument (the object of the relevant class).
Binary operators overloaded through a member function take one explicit argument and those which are overloaded
through a friend function take two explicit arguments.

Operator to Overload Arguments passed to the Member Function Arguments passed to the Friend Function
Unary Operator No 1
Binary Operator 1 2
Types of Inheritance
void daughter :: get ( )
{
father :: get ( ) ;
mother :: get ( ) ;
cout << “child's name: “;
cin >> name;
cout << “child's standard”;
cin >> std;
}
void daughter :: show ( )
{
father :: show ( );
mother :: show ( ) ;
cout << “In child’s name is : “ <<name;
cout << “In child's standard: “ << std;
}
main ( )
{
clrscr ( ) ;
daughter d1;
d1.get ( ) ;
d1.show ( ) ;
}
// Program to show the hierarchical inheritance
#include<iostream.h>
# include<conio. h>
class father //Base class declaration
{
int age;
char name [15];
public:
void get ( )
{
cout<< “father name please”; cin >> name;
cout<< “father’s age please”; cin >> age;
}
void show ( )
{
cout << “father’s name is ‘: “<<name;
cout << “father’s age is: “<< age;
}
};
class son : public father //derived class 1
{
char name [20] ;
int age ;
public;
void get ( ) ;
void show ( ) ;
};
void son : : get ( )
{
father :: get ( ) ;
cout << “your (son) name please” ; cin >>name;
cout << “your age please” ; cin>>age;
}
void son :: show ( )
{
father : : show ( ) ;
cout << “my name is : “ <<name;
cout << “my age is : “ <<age;
}
class daughter : public father //derived class 2.
{
char name [15] ;
int age;
public:
void get ( )
{
father : : get ( ) ;
cout << “your (daughter’s) name please”; cin>>name;
cout << “your age please”; cin >>age;
}
void show ( )
{
father : : show ( ) ;
cout << “my name is: “ << name;
cout << “my age is: “ <<age;
}
};
main ( )
{
clrscr ( ) ;
son S1;
daughter D1 ;
S1. get ( ) ;
D1. get ( ) ;
S1 .show( ) ;
D1. show ( ) ;
}
Hybrid Inheritance
There could be situations where we need to apply two or more types of inheritance to design a program. Basically
Hybrid Inheritance is the combination of one or more types of the inheritance. Here is one implementation of
hybrid inheritance.
//Program to show the simple hybrid inheritance
#include<i sos t ream. h>
#include<conio . h>
class student //base class declaration
{
protected:
int r_no;
public:
void get _n (int a)
{
r_no =a;
}
void put_n (void)
{
cout << “Roll No. : “<< r_no;
cout << “\n”;
}
};
class test : public student //Intermediate base class
{
protected : int part1, part2;
public :
void get_m (int x, int y)
{ parti = x; part 2 = y; }
void put_m (void) {
cout << “marks obtained: “ << “In”
<< “Part 1 = “ << part1 << “in”
<< “Part 2 = “ << part2 << “In”;
}
};
class sports // base for result
{
protected : int score;
public:
void get_s (int s)
{ score = s }
void put_s (void)
{ cout << “ sports wt. : “ << score << “\n\n”; }
};
class result : public test, public sports //Derived from test & sports
{
int total;
public:
void display (void);
};
void result : : display (void)
{
total = part1 + part2 + score;
put_n ( ) ;.
put_m ( );
put_S ( );
cout << “Total score: “ <<total<< “\n”
}
main ( )
{
clrscr ( ) ;
result S1;
S1.get_n (347) ;
S1.get_m (30, 35);
S1.get_s (7) ;
S1.dciplay ( ) ;
}
Virtual Base Classes
The duplication of the inherited members can be avoided
by making common base class as the virtual base class: for
e.g.
class g_parent
{
//Body
};
class parent1: virtual public g_parent
{
// Body
};
class parent2: public virtual g_parent
{
// Body
};
class child : public parent1, public parent2
{
// body
};
The 'child' has two direct base classes ‘parent1’ and ‘parent2’ which themselves has a
common base class ‘grandparent’.

The child inherits the traits of ‘grandparent’ via two separate paths. It can also be inherit
directly as shown by the broken line.

The grandparent is sometimes referred to as ‘INDIRECT BASE CLASS’.


Now, the inheritance by the child might cause some problems.

All the public and protected members of ‘grandparent’ are inherited into ‘child’ twice, first
via ‘parent1’ and again via ‘parent2’.
So, there occurs a duplicacy which should be avoided.

When a class is virtual base class, C++ takes necessary care to see that only one copy of that
class is inherited, regardless of how many inheritance paths exists between virtual base class
and derived class.

The keywords ‘virtual’ and ‘public’ can be used in either order.


//Program to show the virtual base class
#include<iostream.h>
#include<conio . h>
class student // Base class declaration
{ protected: int r_no; public:
void get_n (int a)
{ r_no = a; }
void put_n (void)
{ cout << “Roll No. “ << r_no<< “ln”;}
};
class test : virtual public student // Virtually declared common { //base
class 1
protected:
int part1;
int part2;
public:
void get_m (int x, int y)
{ part1= x; part2=y;}
void putm (void)
{
cout << “marks obtained: “ << “\n”;
cout << “part1 = “ << part1 << “\n”;
cout << “part2 = “<< part2 << “\n”;
}
};
class sports : public virtual student // virtually declared common { //base class 2
protected:
int score;
public:
void get_s (int a) {
score = a ;
}
void put_s (void)
{ cout << “sports wt.: “ <<score<< “\n”;}
};
class result: public test, public sports //derived class
{
private : int total ;
public:
void show (void) ;
};
void result : : show (void)
{ total = part1 + part2 + score ;
put_n ( );
put_m ( );
put_s ( ) ; cout << “\n total score= “ <<total<< “\n” ;
}
main ( )
{
clrscr ( ) ;
result S1 ;
S1.get_n (345)
S1.get_m (30, 35) ;
S1.get-S (7) ;
S1. show ( ) ;
}
//Program to show hybrid inheritance using virtual base classes
#include<iostream.h>
#include<conio.h>
Class A
{
protected:
int x; public:
void get (int) ;
void show (void) ;
};
void A : : get (int a)
{
x=a;
}
void A : : show (void)
{
cout << X ;
}
Class A1 : Virtual Public A
{
protected:
int y ;
public:
void get (int) ;
void show (void);
};
void A1 :: get (int a)
{ y = a;}
void A1 :: show (void)
{
cout <<y ;
{
class A2 : Virtual public A
{
protected:
int z ;
public:
void get (int a)
{ z =a;}
void show (void)
{ cout << z;}
};
class A12 : public A1, public A2
{
int r, t ;
public:
void get (int a)
{ r = a;}
void show (void)
{t= x+ y+z+ r;
cout << “result =” << t ;
}
};
main ( )
{
clrscr ( ) ;
A12 r ;
r.A : : get (3) ;
r.A1 : : get (4) ;
r.A2 : : get (5) ;
r.get (6) ;
r . show ( ) ;
}
REFERENCES:

1.E. Balagurusamy, “Object Oriented Programming with C++”, Fourth edition, TMH,
2008.

2. LECTURE NOTES ON Object Oriented Programming Using C++ by Dr. Subasish Mohapatra,
Department of Computer Science and Application College of Engineering and Technology, Bhubaneswar
Biju Patnaik University of Technology, Odisha

3. K.R. Venugopal, Rajkumar, T. Ravishankar, “Mastering C++”, Tata McGraw-Hill


Publishing Company Limited

4. Object Oriented Programming With C++ - PowerPoint Presentation by Alok Kumar

5. OOPs Programming Paradigm – PowerPoint Presentation by an Anonymous Author

You might also like