Anda di halaman 1dari 36

Object Oriented Programming [3341602]

2 Marks Questions
1. Define Class.
Class is a collection of objects of similar type.
It is a blue print of the real world objects.
Class is a description of objects behavior i.e. it describes objects.
Syntax :
class class_name
{
//body of the class
};
Example :
class abc
{
//body of class
};
2. Define Object.
An instance of a class is called objects. It is run time identifiable entity which
represents characteristics & behavior of class.
Syntax : class_name object_name;
Example:
class_name obj1;
class_name obj1,obj2;

3. Define Data Abstraction. (x2)


It is act of representing important features without giving its explanations. It is one of
the features of OOP.
Classes use the concept of abstraction.
4. Define Inheritance.
It is process by which a class inherits property from other class.
The class who has borrowed/ inherited property is called derived class.
The class from whom the property is inherited is called base class.
Inheritance is used by : symbol.
Syntax :
class derived: [access modifier] class base
{
};

Example:
class derived : public class base
{
};

5. Define Polymorphism.
Polymorphism means same name different forms.
Using this, we can define several operations with same name for different things.
Polymorphism is essential feature of OOP
Types of polymorphism

6. Define Encapsulation.
The wrapping up of data & function into a single unit called class is known as
encapsulation.
The data is accessible only to the functions, which are wrapped in the class.
It done by access modifiers i.e. public, private, protected, friend.
7. Define Dynamic Binding.
Dynamic binding means the linking of object to properties & methods occur at run
time.
It is also known as late binding.
Same name can be used for similar methods on different kinds of objects.
8. What is visibility mode? List different visibility modes supported by C++.
Visibility mode is a mechanism available in OOP & C++ that enables the access of
particular data member & member function of class.
Using visibility modes, one can allow or restrict the access of data & methods of
class.
It is used in inheritance as well in other programming execution in C++ where only
particular methods & properties are allowed for access.

Visibility modes supported by C++:

Private
Public
Protected

9. Explain setw() and setprecision() function.


1)
Setw() is function used to set the width of a data field with some specific width.
It accepts integer argument
Depending upon data passed as argument, C++ will set field width
Syntax : cout<<setw(width)<<data_member;

Example :
int m=1920, n=5;
cout<<setw(6)<<m;
cout<<setw(6)<<n;

Output:
1920
5

2) setprecision() is used to set precision after decimal point in C++ output screen.
It is same as precision() function.
Accepts one integer argument i.e. precision point after decimal.
Syntax :
cout<< setprecision(precision point)<<data_member;

Example :
int m=19.12345787, n=5.15478;
cout<< setprecision (3)<<m;
cout<< setprecision (4)<<n;
Output:
19.123
5.1549

10. Explain setfill() and getline() function.


(1) setfill() is used to fill empty field width provided in setw() function.

Accepts one character argument & fills the empty filed space with that character

Syntax : cout<<setw(width)<<setfill(*)<<value or variable;


Example:
int a = 5;
cout<<setw(10)<<setfill(*)<<a;

Output:
*********5

(2) getline() operates like get() & inserts a null character \0 after the line in the
character array.
Removes delimiter from stream but doesnt store in buffer

Syntax: cin.getline(char *buffer, int length);

Example:
char msg[30];
cout<< type message : ;
cin.getline(msg,30);
cout<<msg;

Output:
Type message : C++
C++

11. What is stream? List C++ stream classes.

A stream is continues flow of character/data. All input & output is performed with
stream.
Sequence of characters organized into lines.
Each line has zero or more characters
Ends with new line character
C++ stream classes:
1. ISTREAM input stream
2. OSTREAM output stream
3. IOSTREAM input output stream
4. STREAMBUF buffered stream

12. What is a scope resolution operator?

Scope resolution operator is used to solve conflicts of scope in terms of data member
as well as methods.
Symbolized with ::
It is also used to access global variables, as well as to solve conflicts of access to
function in hybrid inheritance. Function call be defined outside of class using ::

Syntax: cout<< ::variable;

Example:
int a=25;
int main()
{
int a=10;
cout<<a;
cout<<::a;
getch();
return 0;
}

Output:
10
25

13.

Define constructor
Constructor is a special member function of class, having same name as class
It doesnt have any return type
When a class name is return with () in class, constructor is created.
Must be declared in public section.

Invoked automatically, when object of class is created.


3 types
(1) Default
(2) Parameterized
(3) Copy

Syntax:
class class_name
{
public:
class_name()
{
//constructor of class
}
}

Example:
class abc
{
public:
abc()
{
//constructor of class
}
}

14. Define Pure Virtual Function


Pure virtual function is a virtual function which is declared with value 0.
At the time of creation of virtual function, if 0 value is assigned then pure virtual
function is created.
It is used to create abstract class
If a class has at least on virtual function, then it is said to be abstract class.

Syntax: virtual return-type function_name() = 0;

Example:
class abc
{
public:
virtual void show() = 0; //pure virtual function
}

15. What is global variable?

When a variable is declared outside the scope of class & main() function, it is said to
be global variable.
Global variables can be accessed by any class or main() function.
But is a class or main() function contains same name for other variable, then there
will be a conflict of scope.
To resolve this conflict, we use scope resolution operator ::

Syntax:
Header files
data-type variable-name = value;

Example:
#include<iostream.h>
int a=25;
int main()
{
int a=10;
cout<<a;
cout<<::a;
getch();
return 0;
}

Output:
10
25

16. What is the difference between endl and \n'?


Endl
1 'Endl' is a stream manipulator

17.

\n
1

'\n' is a character like 'k' or '\0'

Essentially a function of iostream


class

Escape sequence

Endl will flush the stream means


empty the screen

Will not flush the screen

If you use cout use endl with it as


it will also flush your stream.

\n will break the string on console


output

Endl is slow compared to \n as it


5 will flush the screen first then will
take you to the new line

\n is faster in execution as it simply


break the line & takes you to new line

How is the expression (m>n?m:n) evaluated?


It is conditional operator which is alternate of if-else conditional structure.
If 1st part is condition
2nd part (?) will check if condition is true or not
If true then 1st argument (m) will be executed
If false then 2nd argument (after : ) will be executed.

Alternative if-else :
if(m>n)
{
m;
}
else
{
n;
}

18. What are header files?


Header files are library files available in C++. Predefined functions are stored in
header files. In C++ program, header files are included to use that predefined
functions.
Included with #include<>
Extension of header file is .h

Syntax : #include<file.h>

Example:
#include<iostream.h>

Some header files are


math.h
iostream.h
string.h
stdio.h
conio.h
stdlib.h

19. Define manipulators and also mention the manipulators that are used in C++

The operators used to format the output displayed on output screen.


It formats/manipulates the output for users to be displayed on console screen.
Manipulators :
endl = insert a new line
setw = set the width of the field for data

20.

Explain the functions : width( ), precision( )


Width() is same as setw() function.
It sets the width for a particular data variable or value.
Accepts one integer argument i.e. width for a data variable.
If data is less in length the defined width, blank space will be added before the data.

Syntax: cout<<width(int d)<<variable or value;

Example:
cout<<width(5)<< C++;
cout<<width(7)<< C++;

Output:
C++
C++

precision() is used to set precision after decimal point in C++ output screen.
It is same as setprecision() function.
Accepts one integer argument i.e. precision point after decimal.
Syntax : cout<< precision(precision point)<<data_member;

Example :
int m=19.12345787, n=5.15478;
cout<< precision (3)<<m;
cout<< precision (4)<<n;

Output:
19.123
5.1549

21. Explain the functions : fill( ), setf( )


Fill() function is used to specify a character that is used to fill the unused portion of
a field.
After using width() function the unused space remains empty, to fill that space we
need fill() function.

Syntax : cout.fill(Character);

Example :
int a=5;
cout.width(5);
cout.fill(*);
cout<<a;

Output:
****5

setf() function is used to specify format flags that can control the form of output
display. Like left or right justification.
Syntax : cout.setf(arg1, arg2);
Example:
char a[20]= c++;
cout.setf(ios::fixed, ios::floatfield);
cout<<a;

22. Explain the role of abstract class while building a class Hierarchy.
OR
What is use of abstract class?
Abstract class is a class which provides base for derived classes in inheritance. When
building class hierarchy i.e. hierarchical inheritance, each class may have different
implementation for a particular function for each derived classes.
To overcome this situation, base class must provide a base where function of a base
class can have different implementation in derived classes.
This can be achieved by providing virtual function in base class. This way abstract
class plays important role in building class hierarchy.
23. Differentiate between the following terms:
a. Data abstraction and Data encapsulation

Data abstraction
It is act of representing important
1 features without giving its
explanations

Data encapsulation
1

The wrapping up of data & function


into a single unit called class is known
as encapsulation.

It means only important features


are exposed to user

It means data is hidden from outside


interference.

Data abstraction is provided by


abstract class

Provided by access modifiers (public,


private, protected)

Inherited derived classes will only have


permission depending upon type of
inheritance.

It provides base for inherited


4
derived classes

b. Inheritance and Polymorphism


Inheritance
When a class borrows properties
1 from other class, this process is
called inheritance

Polymorphism
1

Polymorphism means one name, many


forms.

Inherits properties from


base/parent class

Same function name or operator can


be used for different execution purpose

Some function of base class can be


used in different derived classes

Function has same name but different


implementation in different class.

No pre requirement for this to


happen

Can exist when inheritance is


available.

2 types:
1) Compile time
a) function overloading
b) operator overloading
2) run time
a) virtual function

5 types:
1) Single
2) multiple
5
3) multilevel
4) hierarchical
5) hybird
24. Write the structure of C++ program
Header file
Global variable declaration //optional
Class definition
//optional
Main body of program
{
//scope & code statements
}
Example:
#include<iostream.h>
int main()
{
/* Multi line comment */
// single line comment
cout<< Hello;
getch();
retun 0;
}

25. List advantages of an object oriented programming language (x2)


1) user defined data types can easy be created
2) data security with the help of encapsulation.
3) Existing code can be reused by means of inheritance.
4) Better maintainability of code
5) Better quality of software & less maintenance cost
6) Can easily be upgraded from small to large systems.
7) Easy to partition the work
8) Software complexity can easily managed.
26. Define friend function. What is the use of friend function?
Friend function is a function which is not a member of any particular class but still
we can use the data of class passed in friend function.
Friend function accepts minimum 2 arguments i.e. object of class.
For these classes that function is referred as friend & thats why friend function can
use even private data of those 2 classes.
27. What is the purpose of inline function?
When a function in C++ program is called, it is invoked at the time of call.
This means every time the function is called, compiler has to look for that function in
program, and then search for the arguments in the memory which are passed in
function.
Then passing the control to the function, executing function & coming back from
where it was called.
This creates lot of overhead in a computer while running a program.
When there is need of a function to be called very often, this overhead is a problem.

Inline function is replaced or expanded in the place from where it is called at the
function call time.
This reduces or eliminates the overhead stated above.
So the purpose of inline function is to reduce the calling overhead when there is need
of calling it very often.

28. Define following terms: this pointer (x2), Abstract class


This pointer: This pointer a self reference pointer for a particular class. .
When a member function of class has same argument names as its data member
names, this keyword is used to distinguish between actual data member & argument
passed.
This keyword is also used to return the object of class with (return *this).

Abstract Class: When a class is only used to provide base to its derived class in
inheritance, that base is called abstract.

It only states main features like function declaration & not its explanation
(implementation).
When a class has minimum one pure virtual function, it acts as abstract class.

29. Define following terms: function overriding, pure virtual function


Function overriding:
It is a scenario where a base & derived class have same name of a member function
& creating upon object of derived class, the member function of derived class is
always invoked.
This way member function of base class is always skipped from execution or
overridden by derived class member function.

Pure virtual function: when a member function of a base class is initialized with 0 &
is made virtual, it is called pure virtual function. It is used to make abstract base
class.

30. Define Object Oriented Programming


Object oriented programming is an approach of creating classes for both data &
functions that can be used as templates for creating objects.
Object oriented programming is used to create classes & object to provide some
essential features which other POP languages do not provide.
These features are
1) Data abstraction
2) Data encapsulation
3) Inheritance
4) Polymorphism
5) Data Binding
6) Message passing
31. List out applications of Object Oriented Programming.
1) Graphical user interface design such as windows.
2) Complex real business systems having numerous objects.
3) Simulation & modeling
4) Object oriented database
5) OOP is used in AI, expert system, neural networks, parallel programming
6) CIM/CAD/CAM
32. Types of casting.
I. Implicit Type Cast (Automatic type cast)
II. Explicit Type Cast (Manual type cast)

33. What is an inline function?


Inline function is a function which is replaced or expanded in the place from where it
is called at the function call time.
This reduces or eliminates the overhead of jump from callee function to called
function. When function is called, it is actually replaced with its code there.
34. Define class and object.
A class is a collection of objects of similar type. It is a blue print of the real world
objects. It describes the objects.

Object is an identifiable run time entity with some characteristics & behavior. It is an
instance of the class.

35. Define parameterized constructor


A constructor of a class which can accept arguments is called parameterized
constructor.
It acts as a member function which can accept arguments, but the only difference is
parameterized constructor will be invoked automatically at the time of object
creation.
36. Classifications of data types

Data Types

Basic data types

int, float, double,


char, void

User Defined
Data Types

structure, union,
class, enum

Derived Data
Types

array, function,
pointer reference

37. Basic structure of JAVA programming.


Java Environment
It includes a large number of development tools and hundreds of classes and
methods.

JAVA Development Kit (JDK)


It comes with a collection of tools that are used for developing and running Java
programs. They include :
1) appletviewer
2) javac
3) java
4) jdb
5) javadoc
6) javah

Application Programming Interface (API)


It includes hundred of classes and methods grouped into several functional
packages, most common packages are :
1) Language support package java.lang.*
2) Utilities packages java.util.*
3) I/O packages java.io.*
4) Networking packages java.net.*
5) Database packages java.sql.*

38. Give advantages of JAVA.


1) Complied and interpreted
2) Platform independent
3) Object oriented
4) Robust
5) Secure
6) Distributed
7) Multithreaded and interactive
8) High performance
9) Dynamic and extensible
39. List characteristics of constructor
1) Should be declared in public section.
2) Automatically called when object is declared.
3) Can not return a value.
4) They make implicit calls to the new operator when memory is allocated.
5) One cant declare pointer to constructors.
6) Can not be virtual.
7) Can not be inherited.
8) A class may have several constructors & they are distinguishable by their
argument list.

40. Classify constructors.


Constructor

Implicit
Constructor

Default
Constructor

Explicit
Constructor

Copy
Constructor

No Argument

Parameterized

Copy
Constructor

Dynamic
Constructor

1. Reference variable
#include<iostream.h>
#include<conio.h>
int main()
{
int a;
cin>>a;
int &r=a;
r=r+10;
cout<< r= <<r<<endl;
cout<< a= <<a<<endl;
getch();
return 0;
}

C++ Programs

2. Scope Resolution Operator


#include<iostream.h>
#include<conio.h>
double a=128;
int main()
{
double a=256;
cout<<Local a: <<a<<endl;
cout<< Global a: <<::a<<endl;
getch();
return 0;
}

OUTPUT:
4
r= 40
a=40

OUTPUT:
Local a: 256
Global a:128

3. Memory Management Operator (new and delete)


#include<iostream.h>
#include<conio.h>
int main()
{
int *p;
OUTPUT:
p=new int;
p= 25
*p=25;
cout<< p= <<*p;
delete p;
getch();
return 0;
}

4. Manipulators (endl and setw):#include<iostream.h>


#include<iomanip.h>
int main()
{
int m=1950, n=20, p=5;
cout<< m= <setw(4)<<m<<endl;
cout<< n= <<setw(4)<<n<<endl;
cout<< p= <<setw(4)<<p<<endl;
getch();
return 0;
}
5. Type Casting
#include<iostream.h>
#include<conio.h>
int main()
{
int a=25,b=3;
float c;
c=float(a)/b;
cout<<Division= <<c;
getch();
return 0;
}

OUTPUT:
Division= 8.333333

6. Access Modifiers:
#include<iostream.h>
#include<conio.h>
class abc
{
private:
int x;
protected:
int y;
public:
void add()
{
x=5;
y=10;
int z = x + y;
cout<<"Addition= "<<z<<endl;
}
};

int main()
{
abc obj;
obj.add();
getch();
return 0;
}

OUTPUT:
Addition= 15

7. Static data and Static member functions:


#include<iostream.h>
#include<conio.h>
class abc
{
private:
static int yr;
public:
static void leap()
{
if(yr%4==0)
{
cout<<"Leap Year...";
}
else
{
cout<<"Non-Leap Year...";
}
}
};
OUTPUT:
int abc::yr = 2016;
Leap year...
int main()
{
abc *p=new abc;
p->leap();
getch();
return 0;
}
8. Array Of Objects:
#include<iostream.h>
#include<conio.h>
class abc
{
private:
int x;

public:
void f1()
{
x=5;
cout<<"x= "<<x<<endl;
}

OUTPUT:
x=5
x=5

};
int main()
{
abc obj[2];
for(int i=0;i<=2;i++)
{
obj[i].f1();
}
getch();
return 0;
}
9. 'this' Keyword:
#include<iostream.h>
#include<conio.h>
class abc
{
private :
int x, y;
public:
void f1(int x, int y)
{
int c;
this->x=x;
this->y=y;
c=this->x+ this->y;
cout<<"Addtion= " <<c<<endl;
}
};
int main()
{
int x, y;
cin>>x>>y;
abc *p=new abc;
p->f1(x, y);
getch();
return 0;
}

OUTPUT:
5
10
Addition= 15

10. Recursive Function:#include<iostream.h>


#include<conio.h>
int factorial(int no)
{
if(no>1)
{
return(no*factorial(no-1));
}
else
{
return 1;
}
}
int main()
{
int no;
cin>>no;
cout<<"Factorial: "<<factorial(no);
getch();
return 0;
}
11. Call by Value and Call by Reference:#include<iostream.h>
#include<conio.h>
class abc
{
public:
void call_val(int x, int y)
{
int z;
z=x;
x=y;
y=z;
}
void call_ref(int &x, int &y)
{
int z;
z=x;
x=y;
y=z;
}
};

OUTPUT:5
Factorial: 120

int main()
{
int x,y;
cin>>x>>y;
cout<<"Call by value swap..."<<endl;
abc obj;
obj.call_val(x,y);
cout<<x<<endl<<y<<endl;
cout<<"Call by reference swap..."<<endl;
obj.call_ref(x,y);
cout<<x<<endl<<y<<endl;
getch();
return 0;
}

OUTPUT:
10
5
Call by value swap...
10
5
Call by reference swap...
5
10

12. Inline Function:


#include<iostream.h>
#include<conio.h>
inline int cube(int a)
{
return (a*a*a);
}
int main()
{
int a;
cout<<"Enter no.: ";
cin>>a;
cout<<"Cube: "<<cube(a);
getch();
return 0;
}
13. Friend Function:
#include<iostream.h>
#include<conio.h>
class B;
class A
{
private:
int x;
public:
friend int add(A obj1,B obj2);
void enter_a(int a)

OUTPUT:
Enter no.: 5
Cube: 125

{
x=a;
}
};
class B
{
private:
int y;
public:
friend int add(A obj1, B obj2);

void enter_b(int b)
{
y=b;
}
};
int add(A obj1,B obj2)
{
int z;
z=(obj1.x+obj2.y);
return z;
}
int main()
{
int a,b;
cout<<"Enter two nos.: "<<endl;
cin>>a>>b;
A obj1;
B obj2;
obj1.enter_a(a);
obj2.enter_b(b);
cout<<"Addition: "<<add(obj1,obj2);
getch();
return 0;
}
14. Default Argument:
#include<iostream.h>
#include<conio.h>
int f1(int a, int b=5)
{
return (a+b);
}

OUTPUT:
Enter two nos.:
5
10
Addition: 15

int main()
OUTPUT:
{
30
cout<<f1(10,20)<<endl;
15
cout<<f1(10)<<endl;
getch();
return 0;
}
15. Function Overloading:
#include<iostream.h>
#include<conio.h>
class abc
{
public:
void calc(int a, int b)
{
int c=a+b;
cout<<"Sum: "<<c<<endl;
}
void calc(int a, float b)
{
int c=a/b;
cout<<"Division: "<<c<<endl;
}
void calc(int a)
{
if(a>0)
cout<<"Positive no."<<endl;
}
};
int main()
OUTPUT:
{
10
int a, b;
5
cin>>a>>b;
Sum: 15
abc obj1;
Division: 2
obj1.calc(a, b);
obj1.calc(a, float(b));
Positive
obj1.calc(a);
getch();
return 0;
}

16. Constructor and Destructor :


#include<iostream.h>
#include<conio.h>
class abc
{
private :
int x;
public:
abc()
{
x=5;
}
void print()
{
cout<<"x= "<<x;
}
~abc()
{
cout<<Destructor Called...;
}
};
int main()
OUTPUT:
{
x=5
abc obj;
Destructor Called...
obj.print();
getch();
return 0;
}
17. Copy Constructor :
(program to load binary operator/ program to overload = operator)
#include<iostream.h>
#include<conio.h>
class abc
{
private:
intx,y;
public:
abc(int a, int b)
{
x=a;
y=b;
}

abc(const abc &r)


{
x=r.x;
y=r.y;
cout<<"Copy constructor called..."<<endl;
}
};
int main()
{
abc obj1(10,5);
abc obj2=obj1;
getch();
return 0;
}
18. Single Inheritance:
#include<iostream.h>
#include<conio.h>
class A
{
public:
int x, y;
void set_xy()
{
x=5;
y=10;
}
};
class B : public A
{
int z;
public:
void add()
{
z= x + y;
cout<<z;
}
};
int main()
{
clrscr();
B obj;
obj.set_xy();
obj.add();

OUTPUT:
Copy constructor called...

OUTPUT:
15

getch();
return 0;
}
19. Multiple Inheritance :
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int a;
public:
void get_a()
{
a=5;
}
};
class B
{
protected:
int b;
public:
void get_b()
{
b=10;
}
};
class C : public A, public B
{
int c;
public:
void add()
{
c=a+b;
cout<<c;
}
};

int main()
{
C obj;
obj.get_a();
obj.get_b();
obj.add();
getch();
return 0;
}

OUTPUT:
15

20. Multilevel Inheritance:


#include<iostream.h>
#include<conio.h>
class A
{
protected:
int a;
public:
void set_a()
{
a=5;
}
};
class B : public A
{
protected:
int b;
public:
void set_b()
{
b=10;
}
void add_ab()
{
cout<<"a+b= "<<a+b<<endl;
}
};

class C : public B
{
protected:
int c;
public:
void set_c()
{
c=2;
}
void add_abc()
{
cout<<"a+b+c= "<<a+b+c<<endl;
}
};
int main()
{
C obj;
obj.set_a();
obj.set_b();
obj.set_c();
obj.add_ab();
obj.add_abc();
getch();
return 0;
}
21. Hybrid Inheritance:
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int a;
public:
void set_a()
{
a=5;
}
};

OUTPUT:
a+b= 15
a+b+c= 17

class B:public A
{
protected :
int b;
public:
void set_b()
{
b=10;
}
void add_ab()
{
cout<<"a+b= "<<a+b<<endl;
}
};
class C : public A
{
protected:
int c;
public:
void set_c()
{
c=10;
}
void add_ac()
{
cout<<"a+c= "<<a+c<<endl;
}
};
class D: public B, public C
{
protected:
int d;
public:
void set_d()
{
d=5;
}
void add_abcd()
{
cout<<"a+b+c+d= "<<B::a+b+c+d<<endl;
}
};

int main()
{
D obj;
obj.B::set_a();
obj.set_b();
obj.set_c();
obj.set_d();
obj.add_abcd();
getch();
return 0;
}

OUTPUT:
a+b= 15
a+c= 15
a+b+c+d=30

22. Hierarchical Inheritance:


#include<iostream.h>
#include<conio.h>
class A
{
private:
int a,b;
public:
void set_ab()
{
a=5;
b=15;
}
};
class B : public A
{
public:
void add_ab()
{
cout<<"a+b= "<<a+b<<endl;
}
};
class C : public A
{
public:
void mul_ab()
{
cout<<"a*b= "<<a*b<<endl;
}
};

int main()
{
B objb;
C objc;
objc.set_ab();
objc.mul_ab();
objb.add_ab();
getch();
return 0;
}

OUTPUT:
a+b= 20
a*b= 75

23. Abstract Class:


#include<iostream.h>
#include<conio.h>
class A
{
public:
virtual void f1()=0;
};
class B : public A
{
public:
void f1()
{
cout<<"B Class...";
}
};
int main()
{
B obj;
obj.f1();
getch();
return 0;
}

OUTPUT:
B Class...

24. Operator Overloading (binary operator):


#include<iostream.h>
#include<conio.h>
class abc
{
private :
int x,y;
public:
void set_xy()
{
x=5;
y=10;
}
void operator=(abcobj)
{
x=obj.x;
y=obj.y;
}
void display()
{
cout<<"Value of x: "<<x<<endl;
cout<<"Value of y: "<<y<<endl;
}
};
int main()
{
abc obj1,obj2;
obj1.set_xy();
obj2=obj1;
obj2.display();
getch();
return 0;
}

OUTPUT:
Value of x= 5
Value of y= 10

25. Pure Virtual Function:


#include<iostream.h>
#include<conio.h>
class A
{
public:
virtual void show()=0;
};
class B : public A
{
public:
void show()
{
cout<<"Implementation of Virtual Class...";
}
};
int main()
OUTPUT:
{
Implementation of Virtual Class...
A *obj1;
---(hierarchical inheritance)--B obj2;
obj1=&obj2;
obj1->show();
getch();
return 0;
}
26. Nested Class:
#include<iostream.h>
#include<conio.h>
class A
{
public:
class B : public A
{
private:
int x;
public:
void f1(int a)
{
x = a;
cout<<"x = "<<x;
}
};
};

int main()
{
A::B obj;
obj.f1(5);
getch();
return 0;
}

OUTPUT:
x=5

27. Dynamic Constructor:


#include<iostream.h>
#include<conio.h>
class abc
{
int *x;
public:
abc()
{
x = new int;
*x=5;
cout<<*x;
}
abc(int a)
{
x = new int;
*x=a;
cout<<*x;
}
};
int main()
{
abc obj,obj(15);
getch();
return 0;
}

OUTPUT:
5
15

28. Pointer To Derived Class:


#include<iostream.h>
#include<conio.h>
class A
{
public:
int x;
void f1()
{
x = 5;
cout<<x;
}
};
class B : public A
{
public:
int y;
void f2()
{
y = 10;
cout<<y;
}
};
int main()
{
B *p = new B;
p -> f2();
getch();
return 0;
}

OUTPUT:
10

Anda mungkin juga menyukai