Sessional 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Here are the solutions to the given C++ programming questions:

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;
}

Complex operator+(Complex const &obj) {


Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}

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++;
}

static void display_total_students() {


std::cout << "Total Students: " << total_students <<
std::endl;
}
};

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::string n, int r, float g) {


name = n;
rollNumber = r;
gpa = g;
}
Student(const Student& other) {
name = other.name;
rollNumber = other.rollNumber;
gpa = other.gpa;
}

~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();
}

4. Write a program in C++ to calculate the area of a triangle.


#include <iostream>

class Triangle {
private:
float base, height;

public:
Triangle(float b, float h) {
base = b;
height = h;
}

friend float calculateArea(Triangle t);


};

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;

int MyClass::*p_value = &MyClass::value;


void (MyClass::*p_display)() = &MyClass::display;
void (MyClass::*p_setValue)(int) = &MyClass::setValue;

std::cout << ptr->*p_value << std::endl; // Access and print


initial value
(ptr->*p_display)(); // Call display() function
(ptr->*p_setValue)(20); // Modify value using setValue(int)
(ptr->*p_display)(); // Call display() again to confirm the change
}

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy