0% found this document useful (0 votes)
14 views28 pages

3 - CPP

Uploaded by

gamersdevil6
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)
14 views28 pages

3 - CPP

Uploaded by

gamersdevil6
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/ 28

By

Dr. Arghya Mukherjee


Jr. Lecturer, Dinanath Jr. College & High School,
Dhantoli, Nagpur.
Simple Program Using C++:-
// A program to calculate sum and average of two numbers.
#include<iostream.h> //include file section
#include<conio.h> //include file section
void main() // main function program
{
int a,b; //variable declaration section
clrscr(); //to clear output screen
cout<<“Enter any two values”<<endl; //insert string
cin>>a>>b; // extract values from k/b and store
int c=a+b; //dynamic declaration of variable
int avg=c/2;
cout<<“Sum=“<<c<<endl
<<“Average=“<<avg; // print output into screen.
getch(); //to hold output screen
}
Note:-
The coding for clrscr() and getch() are written in header file “conio.h” and
cin and cout are the objects of class “iostream”.
Structure of C++ program:-
Following is the structure of C++ programming
language.
INCLUDE FILE

CLASS DECLARATION

MEMBER FUNCTION DEFINITION

MAIN FUNCTION PROGRAM


Concept associated with OOps:-
• Class:- Class is the collection of data member and the member
functions. It is a user define data types. It normally contain two
sections that is private as well as public section. The data in the
private section are the private property of class and can not be
accessed by even the object of this class. A class can have many
objects. It always closed with a semicolon (;).
E.g. Class class_name
{
private:
data member
member function
public:
data member
member function
};
Concept associated with OOps:-
• Objects:-
– Objects are the basic runtime entity of an object
oriented system.
– They may represent a person, a place, a bank account,
a table of data or any item that the program has to
handle.
– Object take up a space in memory and have an
associated address.
– Syntax for defining an object is as follows –
class_name object_name;
Concept associated with OOps:-
• Inheritance:-
– Inheritance is the process of creating a new class from existing one.
– The newly created class is referred as derived class which can inherit
the properties of base class.
– The old class is known as base class whose properties is shared by
derived class.
• Polymorphism:-
– Polymorphism means ability to take more than one form.
– An operation may exhibits different behavior depends upon the types
of data used.
– For example if we add two numbers then the result leads to addition,
but if we add two string then the result leads to concatenation.
– Due to polymorphism a single function name can be able to handle
different numbers & types of arguments.
Data abstraction and encapsulation:-
• Encapsulation:-
 The wrapping up of data and function into a single unit (called
class) is known as data encapsulation.
 It is one of the most sticking features of class.
 Data is not accessible by outside world.
 This phenomenon of wrapping up of data from outside class is
known as data hiding or information hiding.
• Data abstraction:-
 Act of representing essential features without including the
background details is known as data abstraction.
 Classes uses the concept of abstraction & are defined as a list of
abstract attributes and functions to operate on these attributes.
 These attributes are called data members and functions are
called member function.
Class & Objects (contd.. ..):-
• The example for class is as follows –
• //A program to read and display the value of data members.
#include<iostream.h> cout<<“Roll=“<<roll_no;
#include<conio.h> }
class stud
{ };
private: void main()
char name[20]; {
int roll_no; stud s;
public:
clrscr();
void read()
{ s.read();
cout<<“Enter data\n”; s.print();
cin>>name>>roll_no; getch();
} }
void print()
{
cout<<“Name=“<<name;
Nesting of member function:-
• Nesting of member function means presence of one member
function within another member function.
#include<iostream.h>
#include<conio.h> cout<<“Name=“<<name;
class stud cout<<“Roll=“<<roll_no;
{ }
private: };
char name[20]; void main()
int roll_no; {
public: stud s;
void read() clrscr();
{ s.print();
cout<<“Enter data\n”; getch();
cin>>name>>roll_no; }
}
void print()
{
read();
Scope resolution operator(contd..):-
• Secondly when we want the facility of writing a member function
outside the class at that time we use scope resolution operator.
• Syntax :- return_type class_name :: function_name(arg);
#include<iostream.h>
#include<conio.h> void stud :: print(void)
class stud {
{ read();
private: cout<<“Name=“<<name;
char name[20]; cout<<“Roll=“<<roll_no;
int roll_no; }
public: void main()
void read() {
{ stud s;
cout<<“Enter data\n”; clrscr();
cin>>name>>roll_no; s.print();
} getch();
void print(void); }
};
Friend function:-
• As we know that it is not possible to access private data member of a class outside the
class, so to makes outside function “friendly” of a class, we have to simply declare
this function as a friend of this class.
• The syntax for declaring friend function as –
friend return_type function_name (arguments);
e.g. class abc
{
-------
public:
-------
friend void xyz(void);
};
• The function declaration should be preceded by keyword friend.
• The function definition does not include either keyword friend or :: operator.
• The friend function can not be called with respect to an object of class.
• Objects are always passed to the friend function as an argument.
• A friend function can be a friend of more than one class.
Constructors:-
• A constructor is a special member function whose task is to initialize the
objects of a class with the default value.
• It is special because its name is same as class name.
• A constructor is invoked when an object of its associated class is created.
• A constructor should be written outside the class by the use of scope
resolution operator.

Properties of constructor:-
• Constructor has no return type.
• Constructor get called only once in the lifetime of an object whenever it
created.
• Constructor can be overloaded.
• Constructor can be written in private section also.
• Constructor name should have same as class name.
• Constructor can take arguments.
Destructors:-
• In programs, memory of object de allocated after the end of
execution. But the memory allocated dynamically remains in the
RAM until the reboot of computers. To avoid these destructor is
used.
• A destructor is also a special member function as its name is similar
to that of class name but preceded by tild (~) operator.
• It is used to destroy the memory created by constructor.

Properties of constructor:-
• Destructor has no return type.
• Destructor get implicitly called for an object before destroying that
object from the memory.
• Destructor contain clean-up code.
• Destructor can not called explicitly.
Destructors (contd..):-
• Example :-
#include<iostream.h> void main()
#include<conio.h> {
int count=0; clrscr();
class ABC {
{ ABC a;
public: {
ABC() cout<<“First Block”<<endl;
{ ABC b;
count++; {
cout<<“object cout<<“Second Block”<<endl;
created=“<<count<<endl; ABC c;
} }
~ABC() }
{ }
cout<<“object getch();
destroyed=“<<count<<endl; }
count--;
}
};
Operator Overloading:-
• Enhancing the capacity of operator to perform a same task with the same
meaning but for the objects is known as operator overloading.
• Only arithmetic operator can be overloaded.
• Operator overloading is of two types as –
 Unary operator overloading
 Binary operator overloading
• Syntax :- return_type operator op (arg);
where, return_type - types of return value.
operator - Keyword
op - symbol of operator
arg - refers to arguments
• Some rules to perform operator overloading are –
• Only the existing operator can be overloaded. We can not create new
operator.
• Meaning and performing task of operator should not be changed.
• Operator overloading can also possible using friend function.
• During operator overloading without friend function we pass no argument
for unary operator and one argument for binary operator. Using friend
function, one argument for unary operator and two for binary operator.
INHERITANCE:-
• The mechanism of creating a new class from existing one is called
inheritance.
• The old class or existing class is known as base class or parent class
• The newly created class is known as derived class or child class.
• By inheritance it is possible to reuse the data of existing class into
new class.
• It is also known as polymorphism.
• There are two forms of information as –
• Private inheritance :- during private inheritance the public
section of base class goes to the private section of derived class.
• Public inheritance :- during public inheritance the public section
of base class goes to the public section of derived class.
• There are five types of inheritance as –
i. Single ii. Multilevel iii. Multiple iv. Hybrid v. Hierarchical
Single Inheritance:-
• Single inheritance occur when there is one base class and one derived
class.
Multilevel Inheritance:-
• When there is one base class and one derived class, which is further derived by
another derived class and so on, is known as multilevel inheritance.
Multiple Inheritance:-
• When there is more than one base class and one derived class at that time such a
concept is known as multiple inheritance.
Hybrid Inheritance:-
• When there is presence of more than one types of inheritance in the program,
such a concept is known as hybrid inheritance.
Hierarchical Inheritance:-
• When there is presence of hierarchy of inheritance of various type at that time
such a concept is known as hierarchical inheritance.
-:POLYMORPHISM:-
• Polymorphism is a Greek word means the ability to take more than
one form.
• Polymorphism plays an important role in allowing objects having
different internal structures to share the same external interface.
• Polymorphism is extensively used in implementing inheritance.
• Polymorphism is categorized into following two categories as –
• Compile time polymorphism
• Run time polymorphism
• Compile time polymorphism is always termed as early binding or
static binding. The example is function overloading & operator
overloading.
• Run time polymorphism is always termed as late binding or
dynamic binding. The example is virtual function.
-:Virtual Function:-
• When we use the same function name in both the
base as well as derived class, the function in base
class is declared as virtual by using the keyword
virtual.

• When a function is made virtual, C++ determines


which function to use at the runtime based on type
of object rather than type of pointer.
-:Rules for Defining Virtual Function:-
• The virtual function must be members of some class.
• They can not be static members.
• They are accessed by using object pointers.
• A virtual function can be a friend of another class.
• A virtual function in base class must be defined even though it may not be used.
• The prototype of the base class virtual function and derived class function must be
identical.
• We can not have virtual constructor but we can have virtual destructor.
• A base pointer can point any type of derived object but reverse is not possible.
-:Pure Virtual Functions:-
• It is normal practice to declare a virtual function inside the base class and re-
define it into derived class.
• The function inside the base class is seldom use of performing any task, it only
serves as a place holder.
• The functions which are only declare but not define in the base class are called
pure virtual function or do-nothing function.
• It can be defined as virtual void get()=0;
• The class containing pure virtual function is called an abstract base class.
-:FILE:-
• Data:- A set of values or simply values is known as data or Raw material of
information.
• Record:- Collection of similar types of data items is called records.
• File:- Collection of similar types of records is known as File.

• File opening method in C++:- In C++ file can be opened in following two way-
• By using constructor.
• By using open() function.
• By using constructor we can open only one file at a time. But if there is
necessity to open more than one file at a time, at that time we use open
function.
• Following is the syntax for opening a file using constructor.
file_stream_class stream_object (file_name);
• Where, file_stream_class can be ifstream or ofstream.
• stream_object can be any valid identifier.
• file_name is the name of the file to be opened.
-:File opening modes:-
• File opening modes are the parameter which is used with the open function to
specify certain additional task while a file is opened.
• Following are the list of file opening modes along with their meaning.
ios :: app :- Append to end of file.
ios :: ate :- Go to end of file on opening.
ios :: binary :- open file in binary mode
ios :: in :- open file in read only mode.
ios :: out :- Open file in writing mode.
ios :: nocreate :- Open fails if the file does not exist.
ios :: noreplace :- Open fails if the file already exist.
ios :: trunc :- Delete content of the file if it exist.
• For ifstream, the default mode is “ios :: in” and for ofstream the default mode is
“ios :: out”.
• It is also possible to combine two or more modes using bitwise OR operator as –
fout.open(“ABC.CPP”,ios :: out | ios :: nocreate);
-:File Pointers and Their Manipulation:-
• Each file has two pointers, one is input pointer (get pointer) and another is output
pointer (put pointer).
• Input pointer is used for reading the contents of given file location and output
pointer is used for writing to a given file location.
• Thus we can use these pointers to move through the file while reading or writing.
• To move file pointers to any desire position, file stream classes support following
functions as –
seekg() :- Moves get pointer (input) to a specified location.
seekp() :- Moves put pointer (output) to a specified location.
tellg() :- Gives the current position of the get pointer.
tellp() :- Gives the current position of the put pointer.
• E.g.
1. fin.seekg(10); :- Moves file pointer to byte no. 10 starting from zero (0).
2. ofstream fout; fout.open(“ABC.CPP”,ios::app); int n=fout.tellp();
Here n will represent number of bytes in file.
3. seekg(offset, reposition); & seekp(offset, reposition);
‘offset’ represent number of bytes to be moved by the file pointer from the
position specified by ‘reposition’.

You might also like