0% found this document useful (0 votes)
4 views22 pages

Object Oriented Programming Using C

The document provides a comprehensive overview of Object-Oriented Programming using C++, covering key concepts such as scope resolution operator, constructors, destructors, dynamic memory management, operator overloading, type conversion, inheritance, and templates. It includes exam-style questions with detailed explanations and examples for each topic. The content is structured into units, each addressing different aspects of C++ programming.

Uploaded by

Utkarsh Razdan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views22 pages

Object Oriented Programming Using C

The document provides a comprehensive overview of Object-Oriented Programming using C++, covering key concepts such as scope resolution operator, constructors, destructors, dynamic memory management, operator overloading, type conversion, inheritance, and templates. It includes exam-style questions with detailed explanations and examples for each topic. The content is structured into units, each addressing different aspects of C++ programming.

Uploaded by

Utkarsh Razdan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

OBJECT ORIENTED PROGRAMMING USING C++

BCA-301 Exam Style Answers (Solved Paper)

Question 1 (Compulsory)

(a) What is Scope Resolution Operator? Explain with example. (2 Marks)

The Scope Resolution Operator (::) in C++ is used to define or access a class member outside
the class. It is also used to access global variables.

Example:

#include<iostream>
using namespace std;

class Test
{
public:
void display();
};

void Test::display()
{
cout << "Hello";
}

int main()
{
Test t;
[Link]();
return 0;
}

(b) Distinguish between local and global class. (2 Marks)

Local Class Global Class


Declared inside a function Declared outside all functions
Scope is limited to function Accessible throughout program
Cannot be used outside function Can be used anywhere
Used for temporary purpose Used for general purpose

(c) Explain new and delete operator. (2 Marks)

The new operator is used to allocate memory dynamically during runtime.


The delete operator is used to release dynamically allocated memory.

Example:

int *p;
p = new int;
delete p;

(d) What is the use of manipulators in C++? (2 Marks)

Manipulators are special functions used to format input and output in C++.

Examples:

 endl → new line


 setw() → set width
 setprecision() → decimal precision

Example:

cout << setw(5) << 10;

(e) Can template function be overloaded? Justify. (2 Marks)

Yes, template functions can be overloaded in C++.

Different template functions can have different parameter lists.

Example:

template<class T>
void show(T a)
{
cout << a;
}

template<class T>
void show(T a, T b)
{
cout << a << b;
}

(f) What is abstract class in C++? (2 Marks)

An abstract class is a class that contains at least one pure virtual function.

Objects of abstract class cannot be created.


It is mainly used for achieving abstraction.

(g) Define pure virtual function. (2 Marks)

A pure virtual function is a virtual function assigned with =0.

Syntax:

virtual void display() = 0;

It makes the class abstract.

(h) Destructors never take any value nor return any value. Justify. (2 Marks)

A destructor is automatically called when an object is destroyed.

Its main purpose is to free memory and resources.

Therefore:

 It does not take arguments.


 It does not return any value.

Syntax:

~Test()
{
}

UNIT – I

Question 2

What do you mean by static data member and static member function in C++? Explain
with suitable example. (16 Marks)

Introduction

In C++, static members belong to the class rather than objects. Only one copy of static data
member exists for all objects.

Static Data Member


A static data member is shared by all objects of the class.

Features

1. Declared using static keyword.


2. Memory allocated only once.
3. Shared among all objects.
4. Initialized outside the class.

Syntax

static int count;

Static Member Function

A static member function can access only static data members.

Features

1. Declared using static keyword.


2. Called using class name.
3. Cannot access non-static members directly.

Syntax

static void show();

Program

#include<iostream>
using namespace std;

class Student
{
static int count;

public:
Student()
{
count++;
}

static void display()


{
cout << "Total Objects = " << count;
}
};
int Student::count = 0;

int main()
{
Student s1, s2, s3;
Student::display();
return 0;
}

Output

Total Objects = 3

Advantages

1. Saves memory.
2. Shared data management.
3. Useful for object counting.
4. Improves efficiency.

Conclusion

Static data members and static member functions are important features of C++ used for
sharing common information among objects.

Question 3(a)

What is the use of Destructor and how are they called and what is the order of calling
many destructors? (8 Marks)

Introduction

Destructor is a special member function used to destroy objects and release memory.

Features of Destructor

1. Name same as class name preceded by ~.


2. No return type.
3. No arguments.
4. Automatically called.
Syntax

~Test()
{
}

Uses of Destructor

1. Release memory.
2. Close files.
3. Free resources.
4. Avoid memory leakage.

Order of Calling Destructor

Destructors are called in reverse order of object creation.

Example

#include<iostream>
using namespace std;

class Test
{
public:
Test()
{
cout << "Constructor Called\n";
}

~Test()
{
cout << "Destructor Called\n";
}
};

int main()
{
Test t1, t2;
return 0;
}

Conclusion

Destructor helps in automatic cleanup of resources and memory management.


Question 3(b)

Discuss the need of constructor in C++. How is it different from normal member
function? Explain its types with examples. (8 Marks)

Introduction

A constructor is a special member function used to initialize objects.

Need of Constructor

1. Initializes object automatically.


2. Saves programming time.
3. Allocates memory.
4. Used for object setup.

Difference between Constructor and Normal Function

Constructor Normal Function


Same name as class Any name
No return type Has return type
Called automatically Called manually
Used for initialization Used for operations

Types of Constructors

1. Default Constructor

class Test
{
public:
Test()
{
cout << "Default Constructor";
}
};

2. Parameterized Constructor

class Test
{
int x;
public:
Test(int a)
{
x = a;
}
};

3. Copy Constructor

class Test
{
int x;
public:
Test(int a)
{
x = a;
}

Test(Test &t)
{
x = t.x;
}
};

Conclusion

Constructors are essential for automatic object initialization and memory management.

UNIT – II

Question 4

Discuss in detail the concept of dynamic memory management using new and delete
operator. Also write the program for the same. (16 Marks)

Introduction

Dynamic memory management means allocating memory during runtime according to


requirement.

C++ provides new and delete operators for this purpose.

Need of Dynamic Memory Allocation


1. Efficient memory usage.
2. Memory allocated when needed.
3. Useful for linked lists, trees etc.
4. Reduces memory wastage.

new Operator

The new operator allocates memory dynamically.

Syntax

pointer = new datatype;

Example

int *p = new int;

delete Operator

The delete operator releases dynamically allocated memory.

Syntax

delete pointer;

Program

#include<iostream>
using namespace std;

int main()
{
int *p;

p = new int;

cout << "Enter Number: ";


cin >> *p;

cout << "Value = " << *p;

delete p;

return 0;
}
Output

Enter Number: 10
Value = 10

Advantages

1. Runtime memory allocation.


2. Flexible memory management.
3. Better memory utilization.
4. Useful for dynamic data structures.

Disadvantages

1. Memory leak possible.


2. Program becomes complex.
3. Improper deletion causes errors.

Conclusion

Dynamic memory management provides flexibility and efficient memory usage in C++
programs.

Question 5(a)

What is operator overloading in C++? Write a program to overload prefix increment


operator using operator overloading in C++. (8 Marks)

Introduction

Operator overloading means giving special meaning to operators for user-defined data types.

Advantages

1. Improves readability.
2. Makes program easy.
3. Supports polymorphism.

Program
#include<iostream>
using namespace std;

class Test
{
int x;

public:
Test()
{
x = 0;
}

void operator ++()


{
++x;
}

void display()
{
cout << "Value = " << x;
}
};

int main()
{
Test t;
++t;
[Link]();
return 0;
}

Output

Value = 1

Conclusion

Operator overloading helps to use operators with objects conveniently.

Question 5(b)

What do you mean by precedence of operator? Discuss the precedence order of


arithmetic operators of C++. (8 Marks)

Introduction
Operator precedence determines the order in which operations are performed.

Arithmetic Operator Precedence

Operator Meaning Priority


() Parenthesis Highest
*, /, % Multiplication, Division, Modulus High
+, - Addition, Subtraction Low

Example

int x = 5 + 2 * 3;

Output:

11

Because multiplication has higher precedence.

Associativity

When precedence is same, associativity decides evaluation order.

Example:

10 - 5 + 2

Evaluated from left to right.

Conclusion

Operator precedence is important for correct expression evaluation.

UNIT – III

Question 6

What do you mean by type conversion? What are different forms of type conversion?
Explain conversion between objects of different classes in detail. (16 Marks)
Introduction

Type conversion means converting one data type into another.

Types of Type Conversion

1. Basic to Basic

Example:

int a = 10;
float b = a;

2. Basic to Class

Conversion from primitive type to class object.

3. Class to Basic

Conversion from object to primitive type.

4. Class to Class

Conversion between objects of different classes.

Conversion Between Objects of Different Classes

It is done using conversion functions or constructors.

Program

#include<iostream>
using namespace std;

class B;

class A
{
int x;

public:
A()
{
x = 10;
}

int getX()
{
return x;
}
};

class B
{
int y;

public:
B()
{
y = 0;
}

void operator = (A a)
{
y = [Link]();
}

void display()
{
cout << "Y = " << y;
}
};

int main()
{
A a;
B b;

b = a;

[Link]();

return 0;
}

Output

Y = 10

Advantages
1. Improves compatibility.
2. Allows flexible programming.
3. Supports user-defined conversions.

Conclusion

Type conversion is an important feature in C++ for handling different data types efficiently.

Question 7(a)

What is Function overriding? Explain with example. (8 Marks)

Introduction

Function overriding occurs when derived class redefines a function of base class.

Features

1. Used in inheritance.
2. Same function name.
3. Same parameters.
4. Achieves runtime polymorphism.

Program

#include<iostream>
using namespace std;

class Base
{
public:
void show()
{
cout << "Base Class";
}
};

class Derived : public Base


{
public:
void show()
{
cout << "Derived Class";
}
};

int main()
{
Derived d;
[Link]();
return 0;
}

Output

Derived Class

Conclusion

Function overriding allows derived class to provide its own implementation.

Question 7(b)

What are the applications of Virtual function? Also describe the limitations of virtual
function. (8 Marks)

Introduction

A virtual function is a member function declared using virtual keyword.

It supports runtime polymorphism.

Applications of Virtual Function

1. Runtime polymorphism.
2. Method overriding.
3. Flexible program design.
4. Used in inheritance.
5. Supports dynamic binding.

Limitations of Virtual Function

1. Slightly slower execution.


2. Increases program complexity.
3. Cannot be static.
4. Constructor cannot be virtual.

Example

class Base
{
public:
virtual void show()
{
cout << "Base";
}
};

Conclusion

Virtual functions are important for runtime polymorphism in C++.

UNIT – IV

Question 8

What do you mean by inheritance? What are different forms of inheritance? Write
program to explain concept of multiple inheritance having role of parameterized
constructors. (16 Marks)

Introduction

Inheritance is the process of deriving a new class from an existing class.

The existing class is called base class and new class is called derived class.

Advantages of Inheritance

1. Code reusability.
2. Reduces redundancy.
3. Improves program structure.
4. Supports hierarchical classification.

Forms of Inheritance
1. Single Inheritance

One derived class from one base class.

2. Multiple Inheritance

One derived class from multiple base classes.

3. Multilevel Inheritance

Derived class becomes base for another class.

4. Hierarchical Inheritance

Multiple derived classes from one base class.

5. Hybrid Inheritance

Combination of different inheritances.

Program for Multiple Inheritance with Parameterized Constructor

#include<iostream>
using namespace std;

class A
{
protected:
int x;

public:
A(int a)
{
x = a;
}
};

class B
{
protected:
int y;

public:
B(int b)
{
y = b;
}
};
class C : public A, public B
{
int sum;

public:
C(int a, int b) : A(a), B(b)
{
sum = x + y;
}

void display()
{
cout << "Sum = " << sum;
}
};

int main()
{
C obj(10,20);
[Link]();
return 0;
}

Output

Sum = 30

Conclusion

Inheritance is one of the most important concepts of object-oriented programming and helps
in code reuse.

Question 9(a)

What are template function and template class in C++? Write a program to swap two
numbers using function template. (8 Marks)

Introduction

Templates allow writing generic programs.

Function Template
Function template allows same function to work with different data types.

Syntax

template<class T>

Class Template

Class template allows creating generic classes.

Program to Swap Two Numbers

#include<iostream>
using namespace std;

template<class T>
void swapNum(T &a, T &b)
{
T temp;
temp = a;
a = b;
b = temp;
}

int main()
{
int x = 10, y = 20;

cout << "Before Swap: " << x << " " << y;

swapNum(x, y);

cout << "\nAfter Swap: " << x << " " << y;

return 0;
}

Output

Before Swap: 10 20
After Swap: 20 10

Advantages

1. Code reusability.
2. Reduces duplication.
3. Generic programming.

Conclusion

Templates are useful for writing flexible and reusable programs.

Question 9(b)

Describe how you would determine number of objects in a file. When do you need such
information? (8 Marks)

Introduction

Objects can be stored in files using file handling in C++.

To determine the number of objects in a file, file size is divided by size of object.

Formula

Number of Objects = File Size / Size of Object

Program

#include<iostream>
#include<fstream>
using namespace std;

class Student
{
int roll;
char name[20];
};

int main()
{
ifstream file("[Link]", ios::binary);

[Link](0, ios::end);

int size = [Link]();

int count = size / sizeof(Student);


cout << "Total Objects = " << count;

[Link]();

return 0;
}

Need of Such Information

1. Database management.
2. Record counting.
3. File organization.
4. Efficient data processing.

Conclusion

Determining the number of objects in a file is useful for handling records efficiently in file
processing.

END OF SOLVED PAPER

You might also like