Anda di halaman 1dari 0

p

a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
UNIT 1
INTRODUCTION TO COMPUTERS:
DEF: - Computer is an Electronic Device Which performs both arithmetic and logical operations when
it was asked in the form of instructions.
When ever a problem evolved in engineering, it can be solved in two ways.
a. By using Human Brain.
b. By using Computer.
If we solve problem by a computer w should create an artificial brain of its own, which is called a
Computer Brain.
DEF: - Programis a collection of instructions which are given to a computer to solve a specific task.
DEF: - Software is a collection of programs.
So to write a program there is a procedure which starts with Algorithm, Flowchart.
DEF: - Algorithmis a step-by-step procedure to solve a problem.
DEF: - Flowchart is a pictorial representation of an Algorithm.
There are 4 types of softwares
a. Operating system software.
b. Utility program software.
c. Language processing software.
d. Application programming software.
Operating system software:
Software that manages the resources of a computer system and schedules its operation is called operating
system software. This will acts an interface to the user and the hardware of the computer.
EX: - 16-bit Microprocessor (MS-DOS), 32-bit Microprocessor (WINDOWS, UNIX, LINUX)
Utility Program Software:
These are Pre-Written program.
Language Processor Software:
These are used to convert the other low languages to the computer understandable language called
Machine Language, also called translator.
There are 2 translators
a. Compiler.
b. Interpreter.
DEF: - compiler is that which checks the program and displays all the errors at a time.
DEF: - Interpreter is that which checks the individual lines of the program and displays the errors if
any then it moves to the next line to check for the errors.
Application Programming Software:
These make the hardware to do useful work.
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
COMPUTER GENERATIONS:
There are 5 generations to explain the evolution of the computer to the present day .
FIRST GENERATION The use of vacuum tubes
SECNOND GENERATION The use of Transistors.
THIRD GENERATION - The use of
FOURTH GENERATION - The use of
FIFTH GENERATION - The use of
LANGUAGE GENERATIONS:
There are 4 language generations.
FIRST GENERATION Developed Machine Level Language
SECOND GENERATION - Developed Assembly Level Language
THIRD GENERATION - Developed High Level Language
FOURTH GENERATION - Developed Application Level Language
The Instruction should be written in any one of the following programming language
a. Machine Level Language.
b. Low Level Language.
c. High Level Language.
Machine Level Language:
A Computer can only understand PULSE for 1 and NON-PULSE for 0. So, in this language all
instructions should be written in binary codec like 0 & 1.
Low Level Language:
Example for a Low Level Language is Assembly language. UNIVAC developed first Symbolic Language
in 1952 used MNEMONICS CODEC rather than numeric codec for operations.
EX: - ADD;
SUB;
These Mnemonics codec must be connected into machine codec; this was done by Assembler. This
language is also known as Low Level Language.
High Level Language:
IBM developed a language called FORTRAN in the year 1957 .These source programs can be run on
different machines using different translators.
High Level Languages can be further divided to
a. Interactive languages.
b. Structured Oriented Languages
c. Object Oriented Languages.
DIFFERENT STRATEGIES OF PROBLEM SOLVING:
There are 2 different problem solving approaches.
a. Structure or procedure oriented programming.
b. Object Oriented Programming.
Structure Oriented Programming:
High Level languages like COBOL, FORTRAN, and C are called as Structured programming languages.
The execution of a problem is done in a sequential manner. The primary focus will be on Functions ,
these functions can be called as the instructions which are organized in such a way which completes the
task.
The communication between each function in this programming is not possible; this is one of the
drawbacks.
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Object Oriented Programming:
OOP is an approach that provides a way of modularizing program by creating partitioned memory area
for both data and functions that can be used as templates for creating copies of such modules on demand.
OOP allows us to decompose a problem into a number of entities called object and then builds data and
functions around those entities.
Difference between Procedure and Object Oriented Languages
Procedure Programming language Object Oriented Programming Language
The major focus is on function The major focus is on Objects
The execution takes place in sequentially manner The execution depends on Objects
This is a TOP-DOWN programming approach This is a BOTTOM-UP programming approach
Data Reusability is not possible Data Reusability is possible
Data Hiding is not possible Data Hiding is possible
It is simple to implement It is very complex to implement
Basic Concepts of OOPS:
a. Object.
b. Class.
c. Data Abstraction and Encapsulation.
d. Inheritance.
e. Polymorphism.
f. Dynamic Binding.
Object :
DEF: Object is a basic Run-Time entity in an object-oriented system.
EX: - a person, student, bank account.
Class:
DEF: Class is a collection of objects of similar type.
EX: fruit mango;
Where mango is an Object and fruit is a Class.
Data Abstraction and Encapsulation:
DEF: Encapsulation is wrapping-up of data and functions into single unit. The data is not accessed outside
world, thats how DATA or INFORMATION HIDING is possible.
DEF: Data Abstraction refers to thee act of representing essential features without including the
background details or explanations.
Inheritance:
DEF: Inheritance Is the process which object of class acquires the properties of object of another class.
So, this concept provides the idea of CODE REUSABILITY.
Polymorphism:
DEF: Polymorphism refers ability to take more than one form. So, this concept provides the concept of
FUNCTION OVERLODING.
Dynamic Binding:
DEF: Dynamic Binding refers to the linking of a procedure call to the code to be executed in response to
the call. Dynamic Binding is also called as RUN-TIME BINDING.
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
History of C++:
C++ is a Object-Oriented Programming Language. Which was developed by Bjarne Stroustrpt at AT&T
BELL Laboratories in Murray Hill, New J ersey, USA, early in 1980s .C++is an extension of of C with a
major addition of the class construct feature of Simula67. Initially it was called as C with Classes .
Structure of c++program:
INCLUDE FILES
CLASS DEFINITION
MAMBER FUNCTION DEFINITION
MAIN FUNCTION
The Program should be saved with an Extension called .CPP .
General Syntax for a class is
class class-name
{
member-data
member functions
} object-name ;
The object name can be created in 3 ways, first is like above and second is
class class-name
{
member-data
member functions
} ;
class class-name object-name ;
void main()
{
.
}
Third way is
class class-name
{
member-data
member functions
} ;
void main()
{
class-name object-name ;
.
}
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Program 1:
Aim: write a c++program which takes values for two variables and use those two variable values to get the
output?
Program:
#include<iostream.h>
#include<conio.h>
class CSE
{
public :
Int a,b;
}Obj;
Void main( )
{
Obj.a=100;
Obj.b=200;
cout<<sum of A and B is :<<(Obj.a+Obj.b);
getch( );
}
Output:
Sum of A and B is : 300
Explanation : A class name called CSE has 2 Public variables a and b and Object named Obj
was created to access the information of the class . In main function we assigned values for a and b by
using Object name called obj as obj.a=100 and obj.b=200 . And I want to perform addition of
those 2 number So, I performed by Obj.a+Obj.b .
Program 2:
Aim: write a c++program which takes student name and roll-number and display the details as output?
Program:
#include<iostream.h>
#include<conio.h>
class CSE
{
public :
char name[20];
Int rollno;
}Obj;
Void main( )
{
cout <<Enter the name of the student ;
cin >>name;
cout <<Enter the roll-number of the student ;
cin >>rollno;
cout<<Name of the student is : <<name<<and his Roll Number is <<rollno;
getch( );
}
Output:
Enter the name of the student : MUKESH
Enter the roll-number of the student : 1229
Name of the student is : MUKESH and his RollNumber is : 1229
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Explanation : A class name called CSE has 2 Public variables rollno of integer datatype and
name[20] of string datatype and Object named Obj was created to access the information of the class .
In main function we passed values for rollno and name by using Object-name called obj with cin
statement . And I want to perform display those 2 .So, I used cout statement .
SCOPE RESOLUTION OPERATOR:
DEF: Scope Resolution Operator ( :: ) is used to define the scope of the variables and the
functions.
By using Scope resolution operator we can declare the variables and the functions inside the Class and we
can define those variables and the functions outside the Class.
Syntax:
class class-name
{
public :
datatype variables
datatype functions ...
}object-name;
datatype class-name :: function-name()
{
.
.
}
void main()
{
function call
}
Program 3:
Aim: Write a c++program to illustrate the use of the scope resolution operator. Without using return?
Program:
#include<iostream.h>
#include<conio.h>
class CSE
{
private :
int a,b;
public:
void sum( );
};
void CSE :: sum( )
{
cout <<Enter the values of A and B , we are using scope resolution operator : ;
cin >>a>>b ;
cout <<SO , the sum of two variables A and B Scope Resolution Operator is <<(a +b) ;
}
class CSE obj;
void main( )
{
clrscr( );
obj.sum ( ) ;
getch( ); }
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Output:
Enter the values of A and B, we are using scope resolution operator: 10 20
So, the sum of two variables A and B without using return is: 30
Explanation:
We are using Scope Resolution Operator to define the function sum ( ) outside the class, in that function
sum ( ) we are accessing the private variables in the class. So, here there is no need for using object-name
because all information is from one class itself.
Program 4:
Aim: Write a c++program to illustrate the use of the scope resolution operator. By using return?
Program:
#include<iostream.h>
#include<conio.h>
class CSE
{
private :
int a,b;
public:
int sum( );
};
int CSE :: sum( )
{
cout <<Enter the values of A and B , we are using scope resolution operator : ;
cin >>a>>b ;
return (a+b) ;
}
class CSE obj;
void main( )
{
clrscr( );
cout <<SO , the sum of two variables A and B Scope Resolution Operator is <<obj.sum() ;
getch( );
}
Output:
Enter the values of A and B, we are using scope resolution operator: 10 20
So, the sum of two variables A and B by using Scope Resolution Operator is: 30
Explanation:
We are using Scope Resolution Operator to define the function sum ( ) outside the class, in that function
sum ( ) we are accessing the private variables in the class. So, here there is no need for using object-name
because all information is from one class itself.
Program 5:
Aim: Write a c++program to illustrate the use of the scope resolution operator. Create a function which
accepts parameters and which returns the value?
Program:
#include<iostream.h>
#include<conio.h>
class CSE
{
private :
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
int a,b;
public:
int sum( int ,int );
};
int CSE :: sum(int x, int y )
{
a=x;
b=y;
cout <<The values of A is :<<a;
cout <<The valuesof Bis :<<b
return (a+b) ;
}
class CSE obj;
void main( )
{
clrscr( );
cout <<SO , the sum of two variables A and B Scope Resolution Operator is <<obj.sum(10 , 20) ;
getch( );
}
Output :
The values of A :10
The values of B :20
SO , the sum of two variables A and B by using Scope Resolution Operator is : 30
Explanation:
We are using Scope Resolution Operator to define the function sum( ) outside the class , in that function
sum( ) we are accessing the private variables in the class . so , here there is no need for using object-
name because all information is from one class itself . here are passing the parameters to the function
sum( ) and it is returning sum of those two variables to the main function .
DYNAMIC MEMORY ALLOCATION:
There are two ways of allocating the memory
a. Static Memory Allocation.
b. Dynamic Memory Allocation.
Static Memory Allocation:
The memory to the variables or the functions will be allocated at the time of compilation (While checking
the errors). There is a problem that the values which were allocated to the variables which were allocated
by using Static memory allocation can be fixed even when the program is terminated. So there may be a
problem that the same value may be assigned to the same variable when it is used in another program.
Dynamic Memory Allocation:
The memory to the variables or the functions will be allocated at the time of executing the program. So,
here the values will be refreshed each and every time when the program got complied. So to allocate the
memory dynamically we should use a keyword called NEW and to deallocate the memory we should use
a keyword called DELETE .
So, to overcome the disadvantage of using a static memory allocation we use dynamic memory allocation.
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Program 6:
Aim: Write a c++program to allocate memory location to a variable by using dynamic memory allocation.
Program:
#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int *p;
p =new int (76);
cout <<SO , the memory location for the variable p Is : <<p <<and the value at that location is <<*p;
delete p;
getch( );
}
Output:
So, the memory location for the variable p is: 0xffac123 and the value at that location is: 76
Explanation:
We are dynamic allocating the memory for the variable P . So to dynamically allocate the variable we
used a keyword NEW, we asking to print the address location for that we are using P and for the value
we using *p . After the use of the dynamic variable allocation we should deallocate that location for that
we will be using a keyword DELETE .
CONSTRUCTOR AND DESTRUCTOR:
Constructor:
DEF: Constructor is a special function which is used to define the variable value with in the class itself.
The constructor will have the same name as that of the class name. The constructor can automatically be
called whenever a new object of the class is created.
Syntax:
class class-name
{
public :
datatype variables1, variables2,
class-name( )
{
variable1 =.;
variable2=;
.
.
.
}
}object-name;
Program 7:
Aim: write ac++program which takes values for two variables using a constructor to get the output?
Program:
#include<iostream.h>
#include<conio.h>
class CSE
{
public :
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Int a,b;
CSE( )
{
a=30;
b=70;
}Obj;
Void main( )
{
cout<<sum of A and B whose values are defined in the class using constructor :<<(Obj.a+Obj.b);
getch( );
}
Output:
Sum of A and B is whose values are defined in the class using constructor: 100
Explanation:
A class name called CSE has 2 Public variables a and b and Object named Obj was created to access
the information of the class. And a constructor has been defined with in the class to initialize the values for
the variables. In main function we are accessing the variables in the class using object-name obj . And I
want to perform addition of those 2 number So, I performed by Obj.a+Obj.b .
Program 8:
Aim: Write a c++program to illustrate the use of the scope resolution operator when there is constructor
defined?
Program:
#include<iostream.h>
#include<conio.h>
class CSE
{
private :
int a,b;
public:
CSE( )
{
a=100;
b=200;
}
int sum( );
};
int CSE :: sum( )
{
cout <<The values of A is :<<a;
cout <<The values of Bis :<<b
return (a+b) ;
}
class CSE obj;
void main( )
{
clrscr( );
cout <<SO , the sum of two variables A and B is : <<obj.sum(10 , 20) ;
getch( );
}
Output:
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
The values of A: 10
The values of B: 20
So, the sum of two variables A and B is: 30
Explanation:
We are using Scope Resolution Operator to define the function sum ( ) outside the class and we also
initialized the variables by using a constructor, in that function sum ( ) we are accessing the private
variables in the class. Here there is no need for using object-name because all information is from one class
itself. The function sum ( ) is displaying the values of the A and B and returning sum of those two
variables to the main function.
Copy Constructor :
DEF: Copy Constructor is a special type of constructor in which new object is created as a copy of
existing object .
In c++ the copy constructor is created when the copy of existing object needs to be created .usually the
compiler creates a copy constructor for each class whenno copy constructor is defined . such a constructor
is called implicit constructor and when the copy constructor is explicitly created in the program the it is
called explicit constructor .
Syntax :
Class_name ( class_name &object)
{
body of the constructor
}
and while invoking the constructor we will use following syntax
class_name new_object_name ( new_object_name);
Program:
Aim: Write a program to illustrate the use of copy constructor ?
Program :
#inlcude<iostream.h>
#include<conio.h>
class test
{
int x;
public :
test( int val)
{
x=val;
}
test(test &obj)
{
x=obj.x;
}
void show( )
{
cout <<x;
}};
void main( )
{
int val;
cout<< enter some number ;
cin >>val;
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
test old(val);
test new1(old);
cout<< the original value;
old.show( );
cout<<the new value is ;
new1.show( );
getch( );
}
Output :
Eneter some number : 200
The original values Is: 2000
The new value is : 2000
Destructor:
DEF: Destructor is also a function which is used to deallocate the memory which was allocated by
using keyword NEW . So, to deallocate the memory we use a keyword DELETE . The destructor will
have the same name as that of the class name. The destructor can be applied only when the variables in the
constructor has been allocated by using dynamic memory allocation.
Syntax:
class class-name
{
public :
datatype *variables1, *variables2,
class-name( )
{
variable1 = new datatype;
variable2=new datatype;
.
.
.
}
~class-name( )
{
delete variable
}}object-name;
Program 9:
Aim: Write a c++program using constructor and destructor ?
Program :
#include<iostream.hng>
#include<conio.h>
#include<new.h>
class CSEB
{
public :
int *p;
CSEB( );
void display( );
~CSEB( );
}object;
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
CSEB :: CSEB( )
{
p=new int;
*p=76;
}
void CSEB : : display()
{
cout<<The location of P is :<<p << and the value of P is <<*p;
}
CSEB : : ~CSEB( )
{
delete p;
}
void main()
{
clrscr( );
object.display( );
getch( );
}
Output:
The location of P is : 0xcfaa12a and the value of P is 76 .
Explanation:
In this program we dynamically allocated memory to the variable P by using a constructor in the class
itself. And I used that value in the function called display and after the use I want to deallocate the
memory that was used to the variable so I deallocated by using destructor and a keyword called DELETE.
INLINE FUNCTION:
DEF: Inline function is a function whose code is copied in the place of each function call.
So, by using the inline function we can overcome the disadvantages of normal functions. Because in
normal functions there should be declaration, function definition and a function call in the main function.
And when ever there is a function call the control flow checks whether the function is declared or not, then
the control flow checks whether it is defined or not and then it will execute the logic.
program 10:
Aim: Write a c++program to illustrate the use of Inline function?
Program :
#include<iostream.h>
#include<conio.h>
inline int sum( int a,int b )
{
cout <<The values of A is :<<a <<endl;
cout <<The values of Bis :<<b <<endl ;
return (a+b) ;
}
void main( )
{
clrscr( );
cout <<SO , the sum of variables for the first function call using inline function : <<sum(10,20) ;
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
cout <<SO , the sum of variables for the 2
nd
function call using inline function : <<sum(30,40) ;
cout <<SO , the sum of variables for the 3
rd
function call using inline function : <<sum(50,60) ;
getch( );
}
Output :
The values of A :10
The values of B :20
SO , the sum of variables for the first function call using inline function : 30
The values of A :30
The values of B :40
SO , the sum of variables for the first function call using inline function : 70
The values of A :50
The values of B :60
SO, the sum of variables for the first function call using inline function: 110
Explanation:
We are using inline function to define the function sum ( ) outside the main function. When ever the
function got called the code of the function will be copied in the place of the function call. So this avoids
the flow of the control from function call to function definition three times.
STATIC CLASS MEMBER:
DEF: Static Class Members refers to the data members which can be accessed without any initiation of
class as an object.
A Static function will have access to only the static variables.
The keyword STATIC can be used on both the variables and the functions .By using STATIC keyword to
variables we can avoid creating the constructor in the class. By using STATIC keyword to function we
can avoid creating the object in the class.
Syntax:
class class-name
{
public :
static datatype variables1, variables2, ;
static function name ;
}}object-name;
Program 11:
Aim: Write a C++program using STATIC variable?
Program:
#include<iostream.h>
#include<conio.h>
class cse
{
public :
static int a;
}obj;
int cse ::a=10;
void main ()
{
cout<<using static variable we can avoid the constructor to initialize the variable in the class:<<obj.a;
}
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Output:
Using static variable we can avoid the constructor to initialize the variable in the class: 10
Explanation:
If we want to avoid creating the constructor, but want to initialize the variable in the class then we can
create by using the static keyword. So, the output will be taken from the variable which was declared
outside the class . As 10.
Program 12
Aim: Write a C++program to illustrate the use STATIC function ?
Program:
#include<iostream.h>
#include<conio.h>
class cse
{
static int a;
public:
static int sum( );
};
int cse ::a=10;
int cse ::sum( );
{
return(a+a);
}
void main ()
{
cout<<static function displays the static variable as output without using object:<<cse :: sum();
}
Output:
Static function displays the static variable as output without using object: 20
Explanation:
If we want to avoid creating the constructor and an object to access the function from a class there is no
need to create the object, but want to initialize the variable in the class then we can create by using the
static keyword . So ,the output will be taken from the varialble which was declared outside the class , and to
get the output we use that function as cse :: sum( ) , So the output will be 10.
THIS POINTER :
DEF: This Pointer is passed as the parameter to the member function call and it is available in the function
definition as a local variable .
This Pointer points to the object for which the member function is called .
Program 13:
Aim: Write a c++program which displays the use of THIS keyword ?
Program :
#include<iostream.h>
#include<conio.h>
class CSEA
{
private :
int num;
public:
void getvalue(int num )
{
this->num=num;
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
}
void printvalue()
{
cout<<the value is <<num;
}};
void main( )
{
test obj;
int num;
clrscr();
cout<< Enter some value;
cin>>num ;
obj.getvalue(num);
obj.printvalue( );
getch( );
}
Output:
Enter some value :10
The Value is : 10
FRIEND KEYWORD:
Friend keyword can be implemented on both the functions and the classes .
a. Friend Function .
b. Friend class .
Friend Function :
DEF: Friend Function is a function that is not a member function of the class but which can access even
the private and protected members if the class .
By using Friend Function we can overcome the use of Scope Resolution Operator to define the function
outside the class .
The keyword used to implement the friend keyword is friend .
Syntax:
class class-name
{
public :
datatype *variables1, *variables2,
friend function name ;
}}object-name;
Program 14:
Aim : Write a c++program to illustrate the use of Friend Function ?
Program :
Class CSEB
{
int a,b;
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
friend int sum();
public :
CSEB() ;
{
a=10;
b=20;
}}obj;
int sum ( )
{
return (obj.a+obj.b);
}
void main()
{
cout<<Using Friend Keyword the sum of two variablesis <<sum( );
}
Friend class :
DEF: Friend class is a class which allows accessing the private data information of the other class.
Syntax:
class class-name
{
public :
datatype *variables1, *variables2,
friend class class-name ;
}}object-name;
Program 15:
Aim : Write a c++program to illustrate the use of Friend class ?
Program :
class CSEB
{
private:
int a,b;
friend class CSEA;
public:
CSEB( )
{
a=10;
b=20;
}}obj;
class CSEA
{
friend int sum();
};
int sum( );
{
return(obj.a+obj.b);
}
void main()
{
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
cout <<Addition of two variable present in the class CSEB is <<sum();
}
Output :
Addition of two variable present in the class CSEB is : 30
Explanation:
We declared the friend class CSEA in the class CSEB , So it helps to access all information that is present
in the class CSEB to class CSEA . So, the values of the variables in the class CSEB a=10 and b=20 can
be access in the class CSEA but we should use the object to access the information .
EXCEPTION HANDLING:
When an avoidable circumstances occur in our program then exceptions are raised by handling the control
to specific functions called handlers . Thus C++provides a build-in error handling mechanism which is
known as exception handling. We use three keyword to handle the Exceptions
a. try .
b. catch.
c. throw.
Syntax :
try
{
throw exception ;
error detection
}
catch( arguments)
{
}
DEF: try block contains the portion of the program that is to be examined for the errors .
DEF : throw block throws the exception that was raised by try
DEF : catch block caught the exception and proceeds with the execution .
The try block should be immediately followed by try block .
Aim : Write a program to implement the keyword like try, catch, throw ?
Program :
#include<iostream.h>
#inlclude<conio.h>
void main( )
{
double I,j;
void divide(double , double );
cout<< Enter the numerator ;
cin>>i;
cout<< Enter the denominator ;
cin >>j;
divide ( i, j) ;
getch( );
}
void divide( double a ,double b)
{
try
{
if( b==0)
throw b;
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
cout<< result is :<<a/b ;
}
catch( double b)
{
cout<<cant divide by zero ;
}}
Output :
Enter the numerator : 10
Enter the denominator : 0
Cant Divide by ZERO .
PREVIOUS PAPER QUESTIONS
1. (a) Can you think of a situation where your program would crash without
reaching the breakpoint which you set at the beginning of main()?
(b) When are copy constructors called?
(c) Can a copy constructor accept an object of the same class as parameter,
instead of reference of the object? [5+5+6]
2. (a) What are some ways try / catch / throw can improve software quality?
(b) How can we handle a constructor that fails?
(c) How can we handle a destructor that fails.
3. (a) What do you mean by Data abstraction?
(b) Difference between C structure and C++structure.
(c) Diffrence between a assignment operator and a copy constructor.
4. (a) What is a friend function? Explain the advantages and disadvantages of it?
(b) What is static member function? Explain its limitations. [8+8]
5. (a) Explain about try, catch, throw keywords in C++?
(b) Write a program to illustrate the exception handling mechanism in C++.
6. (a) What do you mean by Encapsulation and explain in detail.
(b) Explain about friend and inline functions? [8+8]
7. (a) Why should we use iostream instead of the traditional cstdio?
(b) Why does a program go into an infinite loop when someone enters an invalid
input character?
(c) How can we get std::cin to skip invalid input characters?
8. (a) What are the different types of polymorphism?
(b) What are Virtual Functions? How to implement virtual functions inC++
[8+8]
9. (a) What are the differences between a C++struct and C++class?
(b) What is the difference between compiling and linking?
(c) In a large program what problems might occur from putting c++code in
headers?
(d) What is an inline function and when would you use it? [4+4+4+4]
10. (a) Explain the need for Virtual Destructor.
(b) Can we have Virtual Constructors? [8+8]
11. What is the difference between the C++ standard library, and the C++
standard template library?
12. (a) Compare various forms of type cast operations (in C and C++styles). Tell
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
about overloading of these operations.
(b) How to set default values of function arguments? What are pros and contras
of use of this C++opportunity? Whats its alternative?
(c) When writing catch operator we can write directly type of exception as a
type of its argument, pointer to a type of exception or reference to a type of
exception. Compare these approaches. [6+5+5]
13. (a) How a member function of a class is defined?
(b) How data members of a class are defined?
(c) Write a C++program to demonstrate various operations of complex numbers
using class? [4+4+8]
14. (a) Define the following terms:
i. Base class
ii. Derived class
iii. Direct base class
iv. Indirect base class
v. Abstract class
vi. Virtual base class
(b) How do the following two statements differing the operations
i. cin >>c;
ii. cin.get(c); [12+4]
15. (a) What do you mean by dynamic initialization of object? Why do we need to
do this?
(b) How Is dynamic initialize of objects achieved?
(c) Write a C++program for generating Fibonacci series? [4+4+8]
16. (a) What are all the restrictions and limitations in overloading operators?
(b) Write a C++program to demonstrate overloading add(+) and compare (_)
operators on strings? [8+8]
17. (a) Describe the importance of destructors?
(b) What is the copy constructor? What are it uses?
(c) Write a C++function for Tower of Hanoi? [4+4+8]
18. (a) What are the rules for virtual functions?
(b) Write a C++program to demonstrate runtime polymorphism using virtual
function? [8+8]