Sessional 1
Sessional 1
Sessional 1
1. Is it possible to perform addition on two objects of a class? If yes, explain how with the
help of a suitable program.
Yes, it is possible to perform addition on two objects of a class.
One way to achieve this is by defining an overloaded + operator as a member function or a
friend function of the class.
Member Function Approach:
#include <iostream>
class Complex {
public:
int real, imag;
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
void display() {
std::cout << real << " + i" << imag << std::endl;
}
};
int main() {
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
c3.display();
}
2. What are the characteristics of static data members and static member functions?
Provide a program to demonstrate these characteristics.
Static data members and static member functions have the following characteristics:
● Static data members:
○ Belong to the class, not to individual objects.
○ Shared by all objects of the class.
○ Initialized only once, when the first object of the class is created.
○ Can be accessed using the class name or an object of the class.
● Static member functions:
○ Can access only static data members and static member functions of the class.
○ Cannot access non-static members of the class.
○ Can be called using the class name or an object of the class.
#include <iostream>
class Student {
public:
int roll_no;
static int total_students;
Student(int r) {
roll_no = r;
total_students++;
}
int Student::total_students = 0;
int main() {
Student s1(1), s2(2), s3(3);
Student::display_total_students();
}
3. Write a program in C++ to create a class named Student that represents student
information.
#include <iostream>
#include <string>
class Student {
public:
std::string name;
int rollNumber;
float gpa;
Student() {
name = "Unknown";
rollNumber = 0;
gpa = 0.0;
}
~Student() {
std::cout << "Student " << name << " destroyed." << std::endl;
}
void display() {
std::cout << "Name: " << name << std::endl;
std::cout << "Roll Number: " << rollNumber << std::endl;
std::cout << "GPA: " << gpa << std::endl;
}
};
int main() {
Student s1("Alice", 101, 3.8);
Student s2 = s1; // Copy constructor
s1.display();
s2.display();
}
class Triangle {
private:
float base, height;
public:
Triangle(float b, float h) {
base = b;
height = h;
}
float calculateArea(Triangle t) {
return 0.5 * t.base * t.height;
}
int main() {
Triangle t(5, 10);
float area = calculateArea(t);
std::cout << "Area of the triangle: " << area << std::endl;
}
5. Write a C++ program to demonstrate the use of pointers to member data and pointers
to member functions using the operator.
#include <iostream>
class MyClass {
public:
int value;
MyClass(int v) {
value = v;
}
void display() {
std::cout << "Value: " << value << std::endl;
}
void setValue(int v) {
value = v;
}
};
int main() {
MyClass obj(10);
MyClass* ptr = &obj;
Please note:
● These solutions are provided without detailed explanations to save space. Feel free to
ask for more clarification on any specific part.
● You can further optimize and enhance these solutions based on your specific
requirements and coding style.
● Consider using appropriate error handling and input validation techniques to make your
code more robust.
Let me know if you have any other questions or require further assistance!
● http://www.mkjngarment.com/qaaaaaaz/2234385.html
● https://github.com/vivekkumar9919/Brush-up-your-OOPS-knowledge-for-your-2023-intervi
ew-with-this-list-of-50-frequently-asked-question