Oct 24 Bba (CA) CPP Solution
Oct 24 Bba (CA) CPP Solution
Encapsulation
Inheritance
Polymorphism
Abstraction
#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;
}
#include<iostream>
using namespace std;
inline int square(int x) { return x * x; }
int main() {
cout << "Square of 5: " << square(5) << endl;
return 0;
}
#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;
}
#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;
}
class Sample {
public:
~Sample() { cout << "Destructor called" << endl; }
};
#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;
}
1. Single
2. Multiple
3. Multilevel
4. Hierarchical
5. Hybrid
Example:
class Base {
public:
void show() { cout << "Base Class"; }
};
class Derived : public Base {};
#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)
int main() {
cout << sum(5, 10) << endl;
cout << sum(5.5f, 2.2f) << endl;
cout << sum(1, 2, 3) << endl;
return 0;
}
int main() {
Pair<int, double> p(5, 2.5);
cout << p.getFirst() << ", " << p.getSecond() << endl;
return 0;
}
int main() {
ifstream inputFile("input.txt");
ofstream outputFile("output.txt");
string line;
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; }
};
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
Example:
cpp
CopyEdit
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() { cout << "Base 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
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!
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