Assignment 4 C++-1
Assignment 4 C++-1
SET A
Q1
#include <iostream>
class MyNumber {
private:
public:
// Default constructor
MyNumber() {
// Parameterized constructor
MyNumber(int a, int b) {
std::cout << "Average of (" << num1 << ", " << num2 << ", " << num3 << ") is: " << average <<
std::endl;
}
};
int main() {
MyNumber obj3(5, 15); // Parameterized constructor with default value for num3
// Display averages
obj1.displayAverage();
obj2.displayAverage();
std::cout << "Using Parameterized Constructor with Default Value:" << std::endl;
obj3.displayAverage();
return 0;
Q2.
#include <iostream>
#include <string>
class MyDate {
private:
std::string monthNames[12] = {
};
public:
// Parameterized constructor
return;
};
int main() {
std::cin >> day >> separator >> month >> separator >> year;
date.displayDate();
return 0;
Q3
#include <iostream>
class MyPoint {
private:
int x, y;
public:
// Default constructor
MyPoint() {
x=0, y=0;
std::cout << "Default Constructor called: (" << x << ", " << y << ")\n";
}
// Parameterized constructor
MyPoint(int a, int b) {
x=a, y=b;
std::cout << "Parameterized Constructor called: (" << x << ", " << y << ")\n";
// Copy constructor
x = point.x;
y = point.y;
std::cout << "Copy Constructor called: (" << x << ", " << y << ")\n";
std::cout << "Point: (" << x << ", " << y << ")\n";
};
int main() {
MyPoint point1;
MyPoint point3(point2);
// Displaying the points
return 0;
SET B
Q1
#include <iostream>
class MyArray {
private:
int size;
public:
// Dynamic constructor
MyArray(int s) {
size = s;
std::cout << "Array of size " << size << " created.\n";
~MyArray() {
void inputArray() {
if (arr[i] % 2 == 0) {
if (arr[i] % 2 != 0) {
};
int main() {
int n;
std::cin >> n;
MyArray myArray(n);
myArray.inputArray();
myArray.displayEvenOdd();
return 0;
Q2
#include <iostream>
class MyMatrix {
private:
// Dynamic constructor
MyMatrix(int m, int n) {
rows = m;
cols = n;
std::cout << "Matrix of size " << rows << "x" << cols << " created.\n";
~MyMatrix() {
std::cout << "Memory freed for matrix of size " << rows << "x" << cols << ".\n";
void inputMatrix() {
std::cout << "Element [" << i + 1 << "][" << j + 1 << "]: ";
}
}
int sum = 0;
sum += matrix[i][j];
std::cout << "Sum of all elements in the matrix: " << sum << std::endl;
};
int main() {
int m, n;
std::cin >> m;
std::cin >> n;
myMatrix.inputMatrix();
// Display the sum of all elements
myMatrix.displaySum();
return 0;
Q3
#include <iostream>
class MyVector {
private:
public:
// Default constructor
MyVector() {
size=0 ,data=nullptr;
// Parameterized constructor
MyVector(int s) {
size=s;
std::cout << "Parameterized constructor called. Vector size is " << size << ".\n";
~MyVector() {
if (data == nullptr) {
return;
if (i < size - 1) {
};
int main() {
int n;
std::cin >> n;
return 0;
SET C
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
class Student {
private:
int rollNo;
std::string name;
int numSubjects;
float* marks;
public:
// Parameterized constructor
~Student() {
delete[] marks;
void inputMarks() {
std::cout << "Enter marks for " << numSubjects << " subjects: ";
float total = 0;
total += marks[i];
}
// Function to determine class obtained
} else {
return "Fail";
std::cout << "\nPercentage: " << std::fixed << std::setprecision(2) << percentage << "%";
}
};
int main() {
int numStudents;
std::vector<Student> students;
std::string name;
std::cout << "\nEnter details for student " << (i + 1) << ":\n";
std::getline(std::cin, name);
student.inputMarks();
students.push_back(student);
}
// Display details of all students
student.displayDetails();
return 0;