Class Notes
Class Notes
Class Notes
SYLLABUS
UNIT – I
Introduction to fundamental Concepts : Object oriented fundamentals,
Structured verses Object oriented development, elements of object oriented
programming, fundamentals of OOP - class, object, and abstraction and its
importance, encapsulation, polymorphism, benefits of OOP, structure of C++
program.
UNIT – II
Classes and Objects: Working with classes - classes and objects - class
specification, class objects, accessing class members, defining member
functions, inline functions, accessing member functions within class, data hiding,
class member accessibility, empty classes, constructors, parameterized
constructors, constructor overloading, copy constructors, new, delete operators,
“this” pointer, friend classes and friend functions.
UNIT – III
Overloading: Function overloading, operator overloading, overload able
operators, unary operator overloading, operator keyword, limitations of
increment/ decrement operators, binary operator overloading, arithmetic
operators, function templates, class templates.
UNIT – IV
Inheritance: Base class and derived class relationship, derived class
declaration, forms of inheritance, inheritance and member accessibility,
constructors in derived class, destructors in derived class, multiple inheritance,
multi level inheritance, hybrid inheritance, virtual base classes, virtual functions.
UNIT – V
Exception handling and files: Files and streams, opening and closing of files,
file modes, file pointers and manipulation, sequential access to a file, binary file,
random access to a file, error handling during file manipulation, exception
handling, exception handling model, exception handling constructs, list of
exceptions, catching exceptions, handling exceptions.
1
REFERENCE BOOKS:
1. K.R. Venu Gopal, T. Ravishankar, and Raj kumar, “Mastering C++”, Tata
McGraw Hill, 1997.
2. E. Balaguruswamy, “Object Oriented Programming with C++”, Tata
McGraw Hill, 2nd Edition, 2004.
3. Bjarne Stroustrup, “The C++ programming language”, Addison Wesley, 3rd
Edition, 1998.
4. John R Hubbard, Programming with C++, 2nd edition Shaums Outline
Series, McGraw Hill
5. James Martin & james J.Odell, Object Oriented methods – A foundation,
Prentice Hall, 1997
6. Grady Booch, Object Oriented Analysis and Design with application, II
Edition Addision Wesley, 1994
2
UNIT – I (2 MARKS)
2. Here global data is shared by most of Objects are easily communicated with
the functions each other through function.
4. Data cannot be secured and available Data can be secured and can be
to all the function available in the class in which it is
declared
5. Here, the reusability is not possible, Here, we can reuse the existing one
hence redundant code cannot be using the Inheritance concept
avoided.
3
4. Define encapsulation:
5. Define inheritance :
6. Define polymorphism:
7. Define classes:
8. Define object:
Objects are basic runtime entities in an object oriented system. They may
represent a person, a plea, a bank account etc. An instance of the calss is known as
object.
4
1. Write a c++ program to read 2 numbers from the keyboard and display large
value on the screen
#include<iostream.h>
void main()
{
int a,b;
cin>>a>>b;
if(a>b)
cout<<a;
else
cout<<b;
}
Syntax:
Inline function header
{
function body
}
3. When we need inline functions?
Whenever the function is called, it takes a lot of extra time in executing a series of
instructions for tasks such as a jumping to the functions, saving registers, pushing
arguments onto the stack, and returning to the calling function. When the function is
small, a substantial – percentage of execution time may be spent in such overloads.
To eliminate the cost of calls to small functions, We are using Inline functions.
Once an object of a class has been created, there must be a provision to access its
members. This is achieved by using the member access operator, dot(.).
datamember of a class
Name of the class Member access
defined object specifier
5
5. Defining member function
The data members of a class must be declared within the body of the class,
whereas the member functions of the class can be defined in any one of the following
ways.
• Inside the class specification
• Outside the class specification
:: variable name
7. Purpose of setw
setw is one of the manipulator. It is used to specify the width for the value of the
variable and the value is automatically right justified
The main difference between structure and a class in C++ is that, by default, the
members of the class are private, while by default the members of structure are public.
A non-member function can access the private data of the class if it is declared
as a friend to that class. Such a type of function is called friend function.
Syntax.
Class abc
{
public:
friend void xyz(void);
}
Now the function xyz() can access the private data of the class abc
6
A constructor is a special member function whose main operation is to allocate
the required resources such as memory and initialize the objects of its class. The
constructor is invoked whenever an object of its associated class is created.
1.sizeof operator(Sizeof( ))
2.Member access operators(dot operator)
3.Scope Resoulation operator(::)
4.Conditional operator(?:)
}
operator->keyword.
op ->operator to be overloaded.
}
Binary: overloading with a explicit single argument is known as binary operator
overloading.
Syntax:
Return type operator operator symbol(arg)
7
{
}
5. Define templates.
Templates declared for functions are called function templates.Thus we can make
the same function performs the same operation for different datatypes.
Syntax:
Template<class t,>
Return type function name(arg)
{
}
template->keyword.
t->template type argument
arg->one of the arguments must be template data type.
7. Write the function template for swapping two values the values must be generic
datatype.
Template<class t>
Void swap(t x,t y)
{
t t1;
t1=x;
x=y;
y=t1;
Templates declared for classes are called class templates.Thus we can make the
single class to perform the same operation on different data types.
Syntax:
Template<class t>
Class classname
8
{
//class member specification with anonymous type t whenever appropriate
};
9. Define destructor:
A destructor is used to destroy the objects that have been created by the
constructor. Destructor name is same as the class name but it is preceded by the tilde
symbol.
Eg:
Class A
{
public:
~A() // destructor
{
}
};
CONSTRUCTOR DESTRUCTOR
9
void getdata()
{
cin>>a;
}
void putdata()
{
cout<<a;
cout<<this a // can access the value of “a “using “this”
pointer.same as the value of a
}
};
10
3. What do you mean by Hybrid inheritance?
Base A
Derived
B1
Derived
B2
Derived
C
The duplication of member’s can be avoided by making the common base class
as virtual base class while declaring the direct or intermediate base class as
shown below.
grandparent
Parent
parent
child
11
6. What is multilevel inheritance?
The derived class with with more than one base class is
called multiple inheritance
Base class 1
Baseclass2
Derived class
The general syntax for derived class with multiple base class is as follows
body of derived
};
1. What is a stream?
A stream is a sequence of bytes .It acts either as a sourcr from which the input data
can be obtained or as a destination to which the output data can be sent.
The source stream that provides data to the program is called input stream and the
destination stream that receives output from the program is called output stream.
2. Explain how While(fin) statement detects the end of the file connected to the fin
stream?
12
While(fin)
4. .What is an exception?
Exceptions are run time anomalies or unusual conditions that the program may
encounter while executing,Anomalies might include the conditions such as division by
zero,access to an array outside of its bounds etc.
7.. what are all the major elements of the object oriented model?
• Abstraction
• Encapsulation
• Modularity
Hierarchy
13
• Typing
• Concurrency
• Persistence
9.Define abstraction.
An abstraction denotes the essential characteristics of an object that distinguish it
from all other kinds objects and thus provide crisply defined conceptual boundaries,
relative to the perspective of the viewer.
10.Define encapsulation
1. State
2. Behavior
3.Operations
1. Association
2. Inheritance
14
3. Aggregation
4. Using
5. Instantiation
6. Meta-Class
Classical categorization
Conceptual clustering
Prototype Theory
15