CA402 CPP Answers
CA402 CPP Answers
Semester - IV
Answers
This document contains detailed answers to the CA-402 exam paper based on the 2019 pattern. Each
section addresses core concepts with code examples and explanations to assist in exam preparation.
Encapsulation is one of the fundamental concepts of object-oriented programming (OOP). It refers to the
bundling of data and methods that operate on that data into a single unit or class. It restricts direct access to
some of the object's components, helping prevent accidental modification. Data hiding is achieved through
An inline function is a function that is expanded in line when it is invoked. When the inline function is called,
the compiler replaces the function call with the function code itself, saving the overhead of a function call.
A class is a user-defined data type that binds data and functions together. It acts as a blueprint for creating
objects.
Syntax:
class ClassName {
private:
// data members
public:
// member functions
};
Page 1
CA-402: Object Oriented Concepts Through C++
A constructor is a special member function that is automatically invoked when an object of the class is
created. It has the same name as the class and no return type.
2. To access a global variable when a local variable has the same name.
A virtual function is a member function in a base class that can be overridden in a derived class. It is declared
Stream refers to the flow of data. In C++, input and output are performed in the form of streams of bytes
- Private
- Public
- Protected
Page 2
CA-402: Object Oriented Concepts Through C++
Example:
*ptr = 5;
delete ptr;
A friend function can access private and protected members of a class. It is not a member of the class but is
allowed access.
class A {
private:
int x;
public:
A() { x = 10; }
};
void show(A a) {
class Student {
public:
int roll;
};
Page 3
CA-402: Object Oriented Concepts Through C++
Student s[5];
Types:
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
class A {
public:
};
(e) List different types of constructor. Explain any one with example.
Types:
- Default Constructor
- Parameterized Constructor
- Copy Constructor
class A {
public:
};
(a) C++ program to find maximum of two integers using inline function.
Page 4
CA-402: Object Oriented Concepts Through C++
#include<iostream>
return (a > b) ? a : b;
int main() {
int x, y;
return 0;
(b) C++ program to find area of circle and area of triangle using function overloading.
#include<iostream>
int main() {
return 0;
#include<iostream>
int temp = a;
a = b;
b = temp;
Page 5
CA-402: Object Oriented Concepts Through C++
int main() {
int x = 5, y = 10;
swap(x, y);
return 0;
(d) C++ program to create class Employee, accept and display info.
#include<iostream>
class Employee {
public:
int id;
string name;
float salary;
void getdata() {
void display() {
cout << id << " " << name << " " << salary << endl;
};
int main() {
e1.getdata();
e2.getdata();
e1.display();
else
e2.display();
return 0;
Page 6
CA-402: Object Oriented Concepts Through C++
#include<iostream>
class Test {
int a;
public:
};
int main() {
Test t;
t.getdata();
-t;
t.display();
return 0;
- Function Templates
- Class Templates
Page 7
CA-402: Object Oriented Concepts Through C++
Syntax:
ifstream fin("file.txt");
ofstream fout("file.txt");
Types:
#include<iostream>
int main() {
float p, r, t, si;
si = (p * r * t) / 100;
return 0;
#include<iostream>
void stat() {
int m = 0;
static int n = 0;
n++;
Page 8
CA-402: Object Oriented Concepts Through C++
";
int main() {
stat();
stat();
stat();
return 0;
Output:
01
02
03
Conclusion:
This document provides answers and C++ program examples for CA-402. It is designed to help students
Page 9