OBJECT ORIENTED PROGRAMMING
1
OBJECT ORIENTED PROGRAMMING
BOOK REFERENCE- OBJECT ORIENTED PROGRAMMING
WITH C++ - E. BALAGURUSWAMY
IT WAS IMPOSSIBLE TO COMPLITE THIS WORK
WITHOUT THE HELP OF OUR RESPECTIVE TEACHER,
LIPIKA BORUA AND SUJIT ROY.
2
OBJECT ORIENTED PROGRAMMING
Q:01| Draw the basic structure of a C++ program.
Ans: The basic structure of C++ program is given bellow-
Include files
Class declaration
Member functions definitions
Main function program
Q:02| Explain function prototyping with example. Write down the advantages of function
proto typing.
Ans: Function prototyping is one of the major improvements added to C++ functions. The
prototype describes the function interference to the compiler by giving details such as the
number and type of arguments and the type of return values.
Example: float (int x, float y, float z);
The advantages of function prototyping is given bellow-
1. It tells the return type of the data that the function will return.
2. It tells the number of arguments passed to the function.
3. It tells the data types of the each of the passed arguments.
4. Also it tells the order in which the arguments are passed to the function.
Q:03| Explain control structure with example.
Ans: Control structures are the basic entities of a programming language. Control structures
are used to alter the flow of execution of the program.
There are three types of control structure-
1. Sequence structure.
2. Selection structure.
3
OBJECT ORIENTED PROGRAMMING
3. Loop structure.
For example a if-else structure (selection structure) is given bellow-
-----------------
-----------------
If (x==100)
cout<<”x is 100”;
else
cout<<”x is not 100”;
-----------------
-----------------
Q:04| Describe input and output operator with example.
Ans: Input Operator: The input operator, commonly known as the extraction operator (>>), is
used with the standard input stream, cin. The input operator works on two operands, namely,
4
OBJECT ORIENTED PROGRAMMING
the c in stream on its left and a variable on its right. Thus, the input operator takes (extracts)
the value through cin and stores it in the variable.
Output Operator: The output operator, commonly known as the insertion operator (<<), is used
with The standard output stream cout. The output operator works on two operands, namely,
the cout stream on its left and the expression to be displayed on its right. The output operator
directs (inserts) the value to cout.
An example using I/O operators is given below-
-----------------
-----------------
int a;
cin>>a;
a=a+1;
cout<<a;
-----------------
-----------------
Q:05| Define the terms:
i) Data hiding,
ii) Polymorphism
Ans: Data hiding: Data hiding is a software development technique specifically used in object-
oriented programming (OOP) to hide internal object details (data members). Data hiding ensures
exclusive data access to class members and protects object integrity by preventing unintended
or intended changes.
Data hiding also reduces system complexity for increased robustness by limiting
interdependencies between software components.
Data hiding is also known as data encapsulation or information hiding.
5
OBJECT ORIENTED PROGRAMMING
Polymorphism: The word polymorphism means having many forms. Typically, polymorphism
occurs when there is a hierarchy of classes and they are related by inheritance.
C++ polymorphism means that a call to a member function will cause a different function to be
executed depending on the type of object that invokes the function.
Q:06| Describe inline function with example.
Ans: An inline function is a function that is expanded in line when it is invoked. That is, the
compiler replaces the function call with the corresponding function code (something similar to
macros expansion). The inline functions are a C++ enhancement feature to increase the execution
time of a program.
The inline functions are defined as follows-
-----------------
-----------------
inline double cube (double a)
return (a*a*a);
Q:07| Write a C++ program function friendly two classes.
Ans: A C++ program function friendly two classes is given below-
#include <iostream>
using namespace std;
class B;
class A {
private:
int numA;
6
OBJECT ORIENTED PROGRAMMING
public:
A(): numA( 12) { }
friend int add(A, B);
};
class B {
private:
int numB;
public:
B(): numB( 1) { }
friend int add(A , B);
};
int add(A objectA, B objectB)
return (objectA.numA + objectB.numB);
int main()
A objectA;
B objectB;
cout<< "Sum: " << add(objectA, objectB);
return 0;
Q:08| What is inheritance? Explain the various types of inheritance. Explain multiple
inheritance.
7
OBJECT ORIENTED PROGRAMMING
Ans: The mechanism of driving a new class from an old one is called inheritance. The old class is
referred to as the base class and the new one is called the derived class or subclass.
There are five types of inheritance. Such as -
1. A derived class with only one base class, is called single inheritance.
2. A derived class with several base classes, is called multiple inheritance.
3. A traits of one base class may be inherited by more than one derived class, this process is
known as hierarchical inheritance.
4. The mechanism of deriving a class from another derived class is known as multilevel
inheritance.
5. A combination of multiple inheritance and multilevel inheritance is called hybrid inheritance.
Fig: Forms of inheritance.
Multiple inheritance: A derived class with several base classes, is called multiple inheritance.
Example:
-----------------
-----------------
class A
8
OBJECT ORIENTED PROGRAMMING
{
public:
A() { cout << "A's constructor called" << endl; }
};
class B
public:
B() { cout << "B's constructor called" << endl; }
};
class C: public B, public A // Note the or
public:
C() { cout << "C's constructor called" << endl; }
};
int main()
C c;
return 0;
Q:09| What is operator overloading? Write down the rules of operator overloading.
Ans: Operator overloading is function where different operators are applied and depends on the
arguments. Operator- ‘*’ can be used to pass through the function, and it has their own
precedence to execute.
Rules for operator overloading are given bellow-
9
OBJECT ORIENTED PROGRAMMING
1. Only built-in operators can be overloaded. New operators cannot be created.
2. Arity of the operators cannot be changed.
3. Precedence and associativity of the operators cannot be changed.
4. Overloaded operators cannot have default arguments except the function call operator ()
which can have default arguments.
5. Operators cannot be overloaded for built in types only. At least one operand must be
used defined type.
6. Assignment (=), subscript ([]), function call (“()”), and member selection (->) operators
must be defined as member functions.
7. Except the operators specified in point 6, all other operators can be either member
functions or a non-member functions.
8. Some operators like (assignment)=, (address)& and comma (,) are by default overloaded.
Q:10| Describe different types of data types in C++.
Ans: Data types in C++ can be classified under various categories as shown in bellow figure-
Fig: Hierarchy of C++ datatypes.
Q:11| Develop a C++ program to swap two variables using reference variable.
10
OBJECT ORIENTED PROGRAMMING
Ans:
#include <iostream>
using namespace std;
int main()
int a = 5, b = 10, temp;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " <<
temp = a; a = b;
b = temp;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " <<
return 0;
Q:12| Write the characteristics of friend function.
Ans: Characteristics of friend function is given below-
1) Friends should be used only for limited purpose. too many functions or external classes are
declared as friends of a class with protected or private data, it lessens the value of encapsulation
of separate classes in object-oriented programming.
2) Friendship is not mutual. If a class A is friend of B, then B doesn’t become friend of A
automatically.
3) Friendship is not inherited.
4) The concept of friends is not there in Java.
11
OBJECT ORIENTED PROGRAMMING
Q:13| Explain friend class and friend function with example.
Ans: Friend Class: A friend class can access private and protected members of other class in
which it is declared as friend. It is sometimes useful to allow a particular class to access private
members of other class.
Example:
class Node
private:
int key;
Node *next;
friend class LinkedList;
};
Friend function: A friend function of a class is defined outside that class's scope but it has the
right to access all private and protected members of the class. Even though the prototypes for
friend functions appear in the class definition, friends are not member functions.
Example:
class Node
private:
int key;
Node *next;
friend int LinkedList::search();
};
Q:14| Define the importance of destructor function. Give an appropriate example.
12
OBJECT ORIENTED PROGRAMMING
Ans: Destructors have the opposite function of a constructor. The main use of destructors is to
release dynamic allocated memory. Destructors are used to free memory, release resources and
to perform other clean up. Destructors are automatically named when an object is destroyed.
Like constructors, destructors also take the same name as that of the class name.
Example:
-----------------
-----------------
class A{
public:
A() { cout << “A is called” << endl;
~A() { cout << “A is again called” << endl;
};
Q:15| Explain copy constructor with example.
Ans: This is a special constructor for creating a new object as a copy of an existing object. There
will be always only one copy constructor that can be either defined by the user or the system.
Example:
-----------------
-----------------
person q (“Tropa”);
person r (p);
person p = q;
p = q;
-----------------
-----------------
13
OBJECT ORIENTED PROGRAMMING
Q:16| Write down the differences between static and dynamic binding.
Ans: The difference between static and dynamic binding is given bellow-
Basis of Static binding Dynamic binding
comparison
Time of binding Happens at compile time. Happens at run time.
Actual object Actual object is not used for Actual object is used for binding.
binding.
Also known as It is also known as early binding It is also known as late binding
because binding happens during because binding happens at run
compilation. time.
Example Method overloading. Method overriding.
Methods of Private, static and final methods Other than private, static and final
binding show static binding. Because, they methods show dynamic binding.
cannot be overridden. Because, they can be overridden.
Class Type class. Object class.
Q:17| What is function overloading?
Ans: Function overloading is a feature in C++ where two or more functions can have the same
name but different parameters.
Using the concept of function overloading; we can design a family of functions with one function
name but with different arguments lists.
Example:
-----------------
-----------------
int absolute (int);
float absolute (float);
int main ()
14
OBJECT ORIENTED PROGRAMMING
{
a = -5;
b = 5.5;
cout << Absolute value of" <<a<< "=" << absolute (a) << endl;
cout << Absolute value of" <<b<< "=" << absolute (b);
return 0;
int absolute (int var)
if (var<0)
var = -var;
return var;
float absolute (float var)
if (var<0.0)
var = -var;
return var;
Q:18| List the assignment and append operators of string.
Ans: Assignment operators: List the assignment operators is given bellow-
Symbols Meanings
“=” Equal
15
OBJECT ORIENTED PROGRAMMING
“+=” Add Equal
“-=” Subtract Equal
“*=” Multiply Equal
“/=” Division Equal
“%=” Reminder Equal
“&=” And Equal
“|=” Not Equal
Append operators: List the append operators is given bellow-
• String
• Sub-string
• C-string
• Buffer
• Fill
• Range
• Initializer
Q:19| Define member function. Describe how member functions are declared inside the class
and outside the class.
Ans: Member functions are operators and functions that are declared as members of a class.
Member functions do not include operators and functions declared with the friend specifier.
Declaration of member function inside the class:
class Box {
public:
double length;
double breadth;
double height;
16
OBJECT ORIENTED PROGRAMMING
double getVolume(void);
};
Declaration of member function inside the class:
class Box {
public:
double length;
double breadth;
double height;
double getVolume(void) {
return length * breadth * height;
};
Q:20| What is virtual function? Write down the rules pf virtual function.
Ans: A virtual function is a special form of member function that is declared within a base class
and redefined by derived class. The keyword virtual is used to create a virtual function, precede
the function's declaration in the base class. If a class includes a virtual function and if it is
inherited, the virtual class redefines a virtual function to go with it's own need.
Example:
-----------------
-----------------
virtual void SayHello ()
cout<<"Hello from CBaseClass \n \n";
void SayHi()
17
OBJECT ORIENTED PROGRAMMING
{
cout<<"Hi from CBaseClass \n \n";
Rules of virtual function is given bellow-
1. They Must be declared in public section of class
2. Virtual functions cannot be static and also cannot be a friend function of another class.
3. Virtual functions should be accessed using pointer or reference of base class type to
achieve run time polymorphism.
4. The prototype of virtual functions should be same in base as well as derived class.
5. They are always defined in base class and overridden in derived class. It is not mandatory
for derived class to override (or re-define the virtual function), in that case base class
version of function is used.
6. A class may have virtual destructor but it cannot have a virtual constructor.
Q:21| Why virtual function is needed?
Ans: The uses of virtual function is given bellow-
• Virtual functions ensure that the correct function is called for an object, regardless of the
type of reference (or pointer) used for function call.
• They are mainly used to achieve Runtime polymorphism
• Functions are declared with a virtual keyword in base class.
The resolving of function call is done at Run-time.
Q:22| Define Input and output stream. Write the use of put(), write(), get line(), get(),
read().
Ans: Input stream: Input stream objects can read and interpret input from sequences of
characters. Specific members are provided to perform these input operations. The standard
object cin is an object of this type
Output stream: Output stream objects can write sequences of characters and represent other
kinds of data. Specific members are provided to perform these output operations. The standard
objects cout, cerr and clog are objects of this type.
18
OBJECT ORIENTED PROGRAMMING
put() function: put() function is used to write an unformatted character.
write() function: write() function is used to write binary data.
getline() function: getline() function is used to read a string or a line from input stream.
get() function: get() function is used to read a unformatted character.
read() function: read() function is used to read binary data.
Q:23| What is OOP?
Ans: Object oriented programming (OOP) is an approach to program organization and
development that attempts to eliminate some of the pitfalls of conventional programming
methods by incorporating the best of structured programming features with several powerful
new concepts.
Q:24| What is class and object? Explain with example.
Ans: Object: Objects are the basic run-time entities in 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. Objects take up space in the memory and have an associated address.
For example, if "customer" and "account" are two objects in a program, then the customer object
may send a massage to the account object requesting for the bank balance.
Class: Once class has been defined, we can create any number of objects belonging to that class.
Each object is associated with the data of type class with which they are created. A class is thus
a collection of objects of similar types.
Example:
fruit mango;
Will create an object mango belonging to the class fruit.
Q:25| What is operator? Explain different types of operator.
19
OBJECT ORIENTED PROGRAMMING
Ans: An operator is a symbol that tells the computer to perform certain mathematical or logical
manipulations. C++ supports a rich set of built –in operators. Operators are used in programs to
manipulate data and variables. They usually from a part of mathematical or logical expression.
C++ operators can be classified into a number of categories. They include-
01. Arithmetic operators. Ex- +, -, *, /, %.
02. Relational operators. Ex- <, >, <=, >=, ==, !=.
03. Logical operators. Ex- &&, ||, !.
04. Assignment operators. Ex- =, +=, -=, *=, /=, %=, ^=, &=.
05. Increment and decrement operators. Ex- ++i, i++, --i, i--.
06. Conditional operators. Ex- ?, :.
07. Bitwise operators. Ex- &, |, ^, <<, >>, .
08. Special operators. Ex- comma(,), sizeof.
Q:26| What is keyword?
Ans: The keywords implement specific C++ language features. They are explicitly reserved
identifiers and cannot be used as names for the program variables or other user-defined program
elements. Example: default, else, sizeof, void, namespace etc.
Q:27| What is meant by abstraction and encapsulation?
Ans: Data encapsulation led to the important OOP concept of data hiding. Data encapsulation is
a mechanism of bundling the data, and the functions that use them and data abstraction is a
mechanism of exposing only the interfaces and hiding the implementation details from the user.
Q:28| What is reference variable?
Ans: Reference variable provides an alias (alternative name) for a previously defined variable.
Example:
20
OBJECT ORIENTED PROGRAMMING
float total = 100;
float & sum = total;
Q:29| What is binding?
Ans: Binding means matching the function call with the correct function definition by the
compiler. It takes either at compile time or at runtime.
Q:30| What is Scope resolution operator?
Ans: The scope resolution operator (::) is used to identify and disambiguate identifiers used in
different scopes.
Q:30| Write down applications & advantages of OOP?
Ans: Applications of OOP are given bellow-
• Real-time systems
• Simulation and modeling
• Object-oriented databases
• Hypertext, hypermedia and expertext
• All and expert systems
• Neural networks and parallel programing
• Decision support and office automation systems
• CIM/CAM/CAD systems
Advantages of OOP are given below-
• Through inheritance, we can eliminate redundant code and extend the use of existing classes.
21
OBJECT ORIENTED PROGRAMMING
• We can build programs from the standard working modules that communicate with one
another, rather than having to start writing the code from scratch. This leads to saving of
development time and productivity.
• The principle of data hiding helps the programmer to build secure program that cannot be
invaded by code in other parts of the program.
• It is possible to have multiple instances of an object to co-exist without any interference.
• It is possible to map objects in the problem domain to those in the program.
• It is easy to partition the work in.
Q:31| Write down differences between C & C++.
Ans: Differences between C and C++ is given in bellow table-
C C++
1. It was developed in 1969 by Dennis 1. It was developed in 1979 by Bjarne
Ritchie. Stroustrup.
2. Sub set. 2. Super set.
3. Procedural programing. 3. Procedural object oriented programing.
4. Doesn't support information hiding. 4. Supports information hiding.
5. scanf(), printf(). 5. cin, cout.
6. Reference variables are not supported. 6. Reference variables are supported.
7. Virtual and friend functions are not 7. Virtual and friend functions are supported.
supported.
8. main() function can be called from 8. main() function cannot be called from
functions. functions.
9. Inheritance isn't possible. 9. Inheritance is possible.
10. namespace isn't allowed. 10. namespace is allowed.
22
OBJECT ORIENTED PROGRAMMING
Q:32| Define Function call by value
Ans: Function call by value : The call by value method of passing arguments to a function copies
the actual value of an argument into the formal parameter of the function. In this case, changes
made to the parameter inside the function have no effect on the argument.
Example:
-----------------
-----------------
Void swap(int x, int y)
int temp;
temp = x;
x = y;
y = temp;
return;
Q:33| What is the difference between operator overloading as a member function, using friend
function?
Ans: A member function is the one which is defined inside a class and is a member of the class.
It may either be a public private or protected function.
On other hand we use friend function in the case when we want to one class to communicate
with other class. for this we need to declare that friend function in both the classes and define
that friend function outside the associated classes. The friend function is always capable to access
all the members of the associated classes.
Q:34| Applications of C++.
Ans: Applications of C++ are given bellow -
23
OBJECT ORIENTED PROGRAMMING
• Science C++ allows us to create hierarchy-related objects, we can build special object-oriented
libraries which can be used later by many programmers.
• While C++ is able to map the real-world properly, the C part of C++ gives the language the ability
to get close to the machine-level details.
• C++ programs are easily maintainable and expandable. When a new feature needs to be
implemented, it is very easy to add to the existing structure of an object.
• It is expected that C++ will replace C as a general-purpose language in the near future.
Q:35| Describe Tokens
Ans: Tokens: As we know, the smallest individual units in a program is known as tokens. C++ has
the following tokens-
• Keywords
• Identifies
• Constants
• Strings
• Operators
Q:36| Difference between constructor and destructor.
Ans: Difference between constructor and destructor is given bellow table-
Basis for Constructor Destructor
comparison
Purpose It allocates the memory to an object. It deallocates the memory of an
object.
Declaration class_name( arguments if ~ class_name( no arguments
any ){ }; ){ };
24
OBJECT ORIENTED PROGRAMMING
Arguments Constructor accepts argument. Destructor does not accepts any
argument.
Calling Constructor is called automatically, Destructor is called automatically,
while the object is created. as block is exited or program
terminates.
Working Constructor allows object to initialize Destructor allows object to execute
some of its value before, it is used. some code at the time of its
destruction.
Order of Constructor are called in successive Destructor are called in reverse
execution order. order of constructor.
In numbers There can be multiple constructor in There is always a single destructor
the class. in the class.
Copy Copy constructor allows a No such concept.
Constructor constructor to declare and initialize a
object from another object.
Overloading Constructors can be overloaded . Destructor can not be overloaded.
Q:37| Explain different types of file mood.
Ans: Explanation of different different types of file mood are given bellow-
• Opening a File: A file must be opened before you can read from it or write to it. Either
ofstream or fstream object may be used to open a file for writing. And ifstream object is
used to open a file for reading purpose only.
• Closing a File: When a C++ program terminates it automatically flushes all the streams,
release all the allocated memory and close all the opened files. But it is always a good
practice that a programmer should close all the opened files before program
termination.
• Writing to a File: While doing C++ programming, you write information to a file from
your program using the stream insertion operator (<<) just as you use that operator to
output information to the screen. The only difference is that you use an ofstream or
fstream object instead of the cout object.
25
OBJECT ORIENTED PROGRAMMING
• Reading from a File: You read information from a file into your program using the
stream extraction operator (>>) just as you use that operator to input information from
the keyboard. The only difference is that you use an ifstream or fstream object instead
of the cin object.
• File Position Pointers: Both istream and ostream provide member functions for
repositioning the file-position pointer. These member functions are seekg ("seek get")
for istream and seekp ("seek put") for ostream.
Q:38| What is OOPS?
Ans: OOPS is abbreviated as Object Oriented Programming system in which programs are
considered as a collection of objects. Each object is nothing but an instance of a class.
Q:39| Write basic concepts of OOPS.
Ans: Following are the concepts of OOPS-
1. Abstraction.
2. Encapsulation.
3. Inheritance.
Q:40| What are manipulators?
Ans: Manipulators are the functions which can be used in conjunction with the insertion (≪) and
extraction (≫) operators on an object. Examples are endl and setw.
Q:41| Define constructor.
Ans: Constructor is a method use to initialize the state of an object, and it gets invoked at the
time of creation. Rules for constructor are-
• Constructor Name should be same as class name.
• Constructor must have no return type.
26
OBJECT ORIENTED PROGRAMMING
Q:42| Define destructor.
Ans: The destructor is a member function whose name is the same as the class name but
preceded by a tilde. For example, the destructor for the class integer can be defined as shown
bellow-
~ integer(){ };
Q:43| What is an abstract class?
Ans: An abstract class is a class which cannot be instantiated. Creation of an object is not possible
with an abstract class, but it can be inherited. An abstract class can contain only abstract method.
Java allows only abstract method in abstract class while for the language it allows non-abstract
method as well.
Q:44| What are the various types of constructor?
Ans: There are three types of constructor, and they are as follows-
➢ Default constructor- With no parameters.
➢ Parametric constructor- With parameters. Create a new instance of a class and also
passing arguments simultaneously.
➢ Copy constructor- Which creates a new object as a copy of an existing object.
Q:45| What is static and dynamic binding?
Ans: Binding is nothing but the association of a name with the class.
Static binding: Static binding is a binding in which name can be associated with the class during
compilation time, and it is also called as early binding.
Dynamic binding: Dynamic binding is a binding in which name can be associated with the class
execution time, and it is also called as late binding.
27
OBJECT ORIENTED PROGRAMMING
Programs
***A simple C++ program
#include <iostream>
using namespace std;
int main()
cout <<"C++ is better than C. \n";
return 0;
*** An example with C++ program
#include <iostream>
using namespace std;
int main()
float number1, number2, sum, average;
cout <<"Enter two numbers:";
cin >> number1;
cin >> number2;
sum = number1 + number2;
average = sum/2;
cout <<"Sum = " << sum << "\n";
cout <<"Average = " << average << "\n";
return 0;
28
OBJECT ORIENTED PROGRAMMING
}
***Friend function with one class:
#include <iostream>
using namespace std;
class Box
private:
int length;
public:
Box(): length(0) { }
friend int printLength(Box);
};
int printLength(Box b)
b.length += 10;
return b.length;
int main()
Box b;
cout<<"Length of box: "<< printLength(b)<<endl;
return 0;
29
OBJECT ORIENTED PROGRAMMING
***Friend function with two class:
#include <iostream>
class B;
class A
public:
void showB(B& );
};
class B
private:
int b;
public:
B() { b = 0; }
friend void A::showB(B& x);
};
void A::showB(B &x)
std::cout << "B::b = " << x.b;
int main()
A a;
B x;
a.showB(x);
30
OBJECT ORIENTED PROGRAMMING
return 0;
***Swap using reference variable:
#include <iostream>
using namespace std;
int main()
int a = 5, b = 10, temp;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " <<
temp = a; a = b;
b = temp;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " <<
return 0;
***Member function:
#include<iostream>
using namespace std;
class Test {
int value;
public:
31
OBJECT ORIENTED PROGRAMMING
Test(int v = 0) {value = v;}
int getValue() {return value;}
};
int main() {
const Test t;
cout << t.getValue();
return 0;
***Inline function:
#include<iostream>
using namespace std;
inline int Max (int x, int y) {
return (x > y)
int main() {
cout << “Max (20, 10): “ << Max (20, 10) << endl;
cout << “Max (0, 200): “ << Max (0, 200) << endl;
cout << “Max (100, 1010): “ << Max (100, 1010) << endl;
return 0;
***Inheritance:
#include<iostream>
32
OBJECT ORIENTED PROGRAMMING
using namespace std;
class A
public:
A() { cout << "A's constructor called" << endl; }
};
class B
public:
B() { cout << "B's constructor called" << endl; }
};
class C: public B, public A // Note the or
public:
C() { cout << "C's constructor called" << endl; }
};
int main()
C c;
return 0;
***Constructor and destructor:
#include<iostream.h>
using namespace std;
33
OBJECT ORIENTED PROGRAMMING
class student
private:
char name [25];
int roll;
float height, weight;
public:
student ()
strcpy (name, “ram”);
roll=0;
height=0;
weight=0;
void display ()
cout<<”\n name :”<< name;
cout<<”\n roll no”<<roll;
cout<<”\n Height”<<height;
cout<<”\n weight”<<weight;
~ student ()
cout<<”\n destroying object”;
34
OBJECT ORIENTED PROGRAMMING
};
void main ()
student obj;
obj. Student ();
return 0;
35