0% found this document useful (0 votes)
43 views11 pages

Oct 24 Bba (CA) CPP Solution

This document contains an examination paper for S.Y. B.B.A. (Computer Application) on Object Oriented Concepts through C++. It includes various questions on C++ programming concepts such as Object-Oriented Programming, memory management, constructors, inheritance, operator overloading, and exception handling. The paper consists of multiple sections with questions requiring definitions, examples, and explanations of key programming principles.

Uploaded by

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

Oct 24 Bba (CA) CPP Solution

This document contains an examination paper for S.Y. B.B.A. (Computer Application) on Object Oriented Concepts through C++. It includes various questions on C++ programming concepts such as Object-Oriented Programming, memory management, constructors, inheritance, operator overloading, and exception handling. The paper consists of multiple sections with questions requiring definitions, examples, and explanations of key programming principles.

Uploaded by

Ashish Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Total No. of Questions : 5] SEAT No.

[Total No. of Page


PC-1162
[6317]-202
S.Y. B.B.A. (Computer Application)
CA-402:OBJECT ORIENTED CONCEPTS THROUGH CPP
(2019 Pattern) (Semester - IV)

Time : 2½ Hours] [Max. Marks :


70
Instructions to the candidates:
1) All questions are compulsory.
2) Figures to the right side indicate marks.

Q1) Attempt any EIGHT of the following. (Out of Ten) [8 × 2 = 16]

a) What is Object Oriented Programming (OOP)?


Object-Oriented Programming (OOP) is a programming paradigm that is based on the
concept of "objects," which contain both data and methods. It emphasizes modularity,
reusability, and abstraction. Key principles of OOP include:

 Encapsulation
 Inheritance
 Polymorphism
 Abstraction

b) What are applications of C++?


C++ is widely used in various domains, including:

 System software (Operating Systems, Compilers)


 Game development
 Embedded systems
 Database management software
 Web browsers (e.g., Chrome's backend)
 Financial applications

c) Define the scope resolution operator in C++.


The scope resolution operator (::) in C++ is used to define the scope of a function, variable,
or class member outside its declaration. It helps in resolving naming conflicts.
Example:

#include<iostream>
using namespace std;
int a = 10; // Global variable
class Test {
public:
static int a;
};
int Test::a = 20; // Defining static member outside class
int main() {
cout << "Global a: " << ::a << endl; // Accessing global variable
cout << "Class a: " << Test::a << endl; // Accessing class member
return 0;
}

d) List the memory management operators in C++.


The memory management operators in C++ are:

 new (for dynamic memory allocation)


 delete (for deallocation of memory)
 new[] (for array allocation)
 delete[] (for array deallocation)

e) Define inline functions.


Inline functions are functions whose definitions are substituted at the place of their call to
reduce function call overhead. They are defined using the inline keyword. Example:

#include<iostream>
using namespace std;
inline int square(int x) { return x * x; }
int main() {
cout << "Square of 5: " << square(5) << endl;
return 0;
}

f) What are default arguments in C++?


Default arguments are function parameters that assume a predefined value if no argument is
provided during the function call. Example:

#include<iostream>
using namespace std;
void greet(string name = "Guest") {
cout << "Hello, " << name << "!" << endl;
}
int main() {
greet(); // Output: Hello, Guest!
greet("John"); // Output: Hello, John!
return 0;
}

g) Define a class and an object in C++.


A class is a blueprint for creating objects, defining attributes and behaviors. An object is an
instance of a class. Example:

#include<iostream>
using namespace std;
class Car {
public:
string brand;
void show() { cout << "Brand: " << brand << endl; }
};
int main() {
Car obj;
obj.brand = "Toyota";
obj.show();
return 0;
}

h) List the access specifiers in C++.


The access specifiers in C++ are:

 public (Accessible from anywhere)


 private (Accessible only within the class)
 protected (Accessible within the class and derived classes)

i) What is the scope resolution operator in C++?


See answer (c).

j) Define a destructor in C++ with its syntax.


A destructor is a special function that gets called when an object is destroyed. Syntax:

class Sample {
public:
~Sample() { cout << "Destructor called" << endl; }
};

Q2) Attempt any four of the following. (Out of Five) [4 × 4 = 16]

a) What are manipulators in C++? Give an example.


Manipulators are used to format the output in C++. Example:

#include<iostream>
#include<iomanip>
using namespace std;
int main() {
cout << setw(10) << "Hello" << endl;
cout << setprecision(2) << 3.14159 << endl;
return 0;
}

b) Define reference variable in C++ and explain its use with an example.
A reference variable is an alias for another variable. Example:

#include<iostream>
using namespace std;
int main() {
int x = 10;
int &y = x; // Reference variable
y = 20;
cout << "x = " << x << endl; // Output: 20
return 0;
}

c) Define Constructor. Explain any of its type along with example.


A constructor is a special function that initializes an object. Parameterized Constructor
Example:
#include<iostream>
using namespace std;
class Test {
public:
int a;
Test(int x) { a = x; }
void show() { cout << "a = " << a << endl; }
};
int main() {
Test obj(5);
obj.show();
return 0;
}

d) Define inheritance and its types in C++.


Inheritance allows a class to derive properties from another class. Types:

1. Single
2. Multiple
3. Multilevel
4. Hierarchical
5. Hybrid

Example:

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

e) Explain operator overloading in C++ with an example.


Operator overloading allows redefining operators for user-defined types. Example:

#include<iostream>
using namespace std;
class Complex {
public:
int real, img;
Complex operator+(Complex obj) {
Complex temp;
temp.real = real + obj.real;
temp.img = img + obj.img;
return temp;
}
};
Solutions to C++ Questions (Q4)

a) Function Overloading in C++


The following program demonstrates function overloading using three different sum() functions.
#include <iostream>
using namespace std;

int sum(int a, int b) {


return a + b;
}

float sum(float a, float b) {


return a + b;
}

int sum(int a, int b, int c) {


return a + b + c;
}

int main() {
cout << sum(5, 10) << endl;
cout << sum(5.5f, 2.2f) << endl;
cout << sum(1, 2, 3) << endl;
return 0;
}

b) Class Template 'Pair'


The following class template stores a pair of values of different types.
#include <iostream>
using namespace std;

template <typename T1, typename T2>


class Pair {
private:
T1 first;
T2 second;
public:
Pair(T1 f, T2 s) : first(f), second(s) {}
void setFirst(T1 f) { first = f; }
void setSecond(T2 s) { second = s; }
T1 getFirst() { return first; }
T2 getSecond() { return second; }
};

int main() {
Pair<int, double> p(5, 2.5);
cout << p.getFirst() << ", " << p.getSecond() << endl;
return 0;
}

c) File Handling: Remove Blank Lines


This program reads from 'input.txt', removes blank lines, and writes to 'output.txt'.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
ifstream inputFile("input.txt");
ofstream outputFile("output.txt");
string line;

if (inputFile && outputFile) {


while (getline(inputFile, line)) {
if (!line.empty()) {
outputFile << line << endl;
}
}
}
inputFile.close();
outputFile.close();
return 0;
}

d) Shape Class and Inheritance


This program demonstrates inheritance with the 'Shape', 'Rectangle', and 'Circle' classes.
#include <iostream>
using namespace std;

class Shape {
protected:
string color;
public:
Shape(string c) : color(c) {}
virtual string getColor() { return color; }
};
class Rectangle : public Shape {
private:
double width, height;
public:
Rectangle(string c, double w, double h) : Shape(c), width(w), height(h) {}
double getArea() { return width * height; }
string getColor() override { return "Rectangle color: " + color; }
};

class Circle : public Shape {


private:
double radius;
public:
Circle(string c, double r) : Shape(c), radius(r) {}
double getArea() { return 3.14159 * radius * radius; }
string getColor() override { return "Circle color: " + color; }
};

int main() {
Rectangle r("Red", 4, 5);
Circle c("Blue", 3);

cout << r.getColor() << ", Area: " << r.getArea() << endl;
cout << c.getColor() << ", Area: " << c.getArea() << endl;

return 0;
}
Q5) Short Notes
(a) Virtual Functions

 A virtual function in C++ allows a function to be overridden in derived classes while


maintaining dynamic (runtime) polymorphism.
 It enables late binding, where function calls are resolved at runtime instead of
compile time.
 Declaring a function as virtual in the base class ensures that when a derived class
overrides it, the correct function is called even when accessed via a base class pointer.

Example:

cpp
CopyEdit
#include <iostream>
using namespace std;

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

class Derived : public Base {


public:
void show() override { cout << "Derived class function" << endl; }
};

int main() {
Base* bptr;
Derived d;
bptr = &d;
bptr->show(); // Calls Derived's show() due to virtual function
return 0;
}

Output:

kotlin
CopyEdit
Derived class function

(b) Exception Handling

 Exception handling in C++ helps detect and handle runtime errors gracefully using
try, catch, and throw blocks.
 It prevents program crashes and allows controlled execution flow even in error
scenarios.

Example:

cpp
CopyEdit
#include <iostream>
using namespace std;

int main() {
try {
int a = 5, b = 0;
if (b == 0) throw "Division by zero error!";
cout << a / b;
}
catch (const char* msg) {
cout << "Exception caught: " << msg << endl;
}
return 0;
}

Output:

vbnet
CopyEdit
Exception caught: Division by zero error!

(c) Friend Function

 A friend function in C++ allows access to the private and protected members of a
class without being a member of that class.
 It is declared inside a class using the keyword friend.

Example:

cpp
CopyEdit
#include <iostream>
using namespace std;

class Sample {
private:
int data;
public:
Sample(int d) : data(d) {}
friend void display(Sample s);
};

void display(Sample s) {
cout << "Private data: " << s.data << endl;
}

int main() {
Sample obj(42);
display(obj); // Accessing private data through a friend function
return 0;
}

Output:

kotlin
CopyEdit
Private data: 42

You might also like