0% found this document useful (0 votes)
3 views

Assignment 4 C++-1

The document contains multiple C++ programming assignments focusing on object-oriented concepts such as constructors, destructors, and dynamic memory allocation. Each assignment includes classes for handling numbers, dates, points, arrays, matrices, vectors, and student records, with functionalities for input, output, and calculations. The code demonstrates the implementation of various constructors, memory management, and basic data processing techniques.

Uploaded by

Hari Kalu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment 4 C++-1

The document contains multiple C++ programming assignments focusing on object-oriented concepts such as constructors, destructors, and dynamic memory allocation. Each assignment includes classes for handling numbers, dates, points, arrays, matrices, vectors, and student records, with functionalities for input, output, and calculations. The code demonstrates the implementation of various constructors, memory management, and basic data processing techniques.

Uploaded by

Hari Kalu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Assignment 4

SET A

Q1

#include <iostream>

class MyNumber {

private:

int num1, num2, num3;

public:

// Default constructor

MyNumber() {

num1=0, num2=0, num3=0;

// Parameterized constructor

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

num1=a, num2=b, num3=c;}

// Parameterized constructor with default values

MyNumber(int a, int b) {

num1=a, num2=b, num3=0;} // num3 defaults to 0

// Function to calculate and display the average

void displayAverage() const {

double average = (num1 + num2 + num3) / 3.0;

std::cout << "Average of (" << num1 << ", " << num2 << ", " << num3 << ") is: " << average <<
std::endl;

}
};

int main() {

// Create objects using different constructors

MyNumber obj1; // Default constructor

MyNumber obj2(10, 20, 30); // Parameterized constructor

MyNumber obj3(5, 15); // Parameterized constructor with default value for num3

// Display averages

std::cout << "Using Default Constructor:" << std::endl;

obj1.displayAverage();

std::cout << "Using Parameterized Constructor:" << std::endl;

obj2.displayAverage();

std::cout << "Using Parameterized Constructor with Default Value:" << std::endl;

obj3.displayAverage();

return 0;

Q2.

#include <iostream>

#include <iomanip> // for std::setw and std::setfill

#include <string>

class MyDate {
private:

int dd, mm, yyyy;

std::string monthNames[12] = {

"Jan", "Feb", "Mar", "Apr", "May", "Jun",

"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"

};

public:

// Parameterized constructor

MyDate(int day, int month, int year) {

dd=day, mm=month, yyyy=year;}

// Function to display date in dd-Mon-yyyy format

void displayDate() const {

// Check for valid month

if (mm < 1 || mm > 12) {

std::cout << "Invalid month!" << std::endl;

return;

std::cout << std::setw(2) << std::setfill('0') << dd << "-"

<< monthNames[mm - 1] << "-"

<< yyyy << std::endl;

};

int main() {

int day, month, year;


// Input date in dd-mm-yyyy format

std::cout << "Enter date (dd-mm-yyyy): ";

char separator; // To handle the '-' separator

std::cin >> day >> separator >> month >> separator >> year;

// Create an object of MyDate using dynamic initialization

MyDate date(day, month, year);

// Display the formatted date

std::cout << "Output: ";

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

MyPoint(const MyPoint &point) {

x = point.x;

y = point.y;

std::cout << "Copy Constructor called: (" << x << ", " << y << ")\n";

// Function to display the point

void display() const {

std::cout << "Point: (" << x << ", " << y << ")\n";

};

int main() {

// Using default constructor

MyPoint point1;

// Using parameterized constructor

MyPoint point2(10, 20);

// Using copy constructor

MyPoint point3(point2);
// Displaying the points

std::cout << "\nDisplaying Points:\n";

point1.display(); // Point initialized with default constructor

point2.display(); // Point initialized with parameterized constructor

point3.display(); // Point copied using copy constructor

return 0;

SET B

Q1

#include <iostream>

class MyArray {

private:

int* arr; // Pointer to the dynamic array

int size;

public:

// Dynamic constructor

MyArray(int s) {

size = s;

arr = new int[size]; // Allocate memory for the array

std::cout << "Array of size " << size << " created.\n";

// Destructor to free memory

~MyArray() {

delete[] arr; // Deallocate memory


std::cout << "Memory freed for array of size " << size << ".\n";

// Function to accept array elements

void inputArray() {

std::cout << "Enter " << size << " elements:\n";

for (int i = 0; i < size; ++i) {

std::cin >> arr[i];

// Function to display even and odd numbers

void displayEvenOdd() const {

std::cout << "Even numbers: ";

for (int i = 0; i < size; ++i) {

if (arr[i] % 2 == 0) {

std::cout << arr[i] << " ";

std::cout << "\nOdd numbers: ";

for (int i = 0; i < size; ++i) {

if (arr[i] % 2 != 0) {

std::cout << arr[i] << " ";

std::cout << std::endl;

};
int main() {

int n;

// Input size of the array

std::cout << "Enter size of the array: ";

std::cin >> n;

// Create an object of MyArray using dynamic constructor

MyArray myArray(n);

// Input elements into the array

myArray.inputArray();

// Display even and odd numbers

myArray.displayEvenOdd();

return 0;

Q2

#include <iostream>

class MyMatrix {

private:

int** matrix; // Pointer to a dynamic 2D array

int rows, cols;


public:

// Dynamic constructor

MyMatrix(int m, int n) {

rows = m;

cols = n;

matrix = new int*[rows]; // Allocate memory for rows

for (int i = 0; i < rows; ++i) {

matrix[i] = new int[cols]; // Allocate memory for each column

std::cout << "Matrix of size " << rows << "x" << cols << " created.\n";

// Destructor to free memory

~MyMatrix() {

for (int i = 0; i < rows; ++i) {

delete[] matrix[i]; // Deallocate memory for each row

delete[] matrix; // Deallocate memory for the row pointers

std::cout << "Memory freed for matrix of size " << rows << "x" << cols << ".\n";

// Function to input matrix elements

void inputMatrix() {

std::cout << "Enter elements of the matrix:\n";

for (int i = 0; i < rows; ++i) {

for (int j = 0; j < cols; ++j) {

std::cout << "Element [" << i + 1 << "][" << j + 1 << "]: ";

std::cin >> matrix[i][j];

}
}

// Function to calculate and display the sum of all elements

void displaySum() const {

int sum = 0;

for (int i = 0; i < rows; ++i) {

for (int j = 0; j < cols; ++j) {

sum += matrix[i][j];

std::cout << "Sum of all elements in the matrix: " << sum << std::endl;

};

int main() {

int m, n;

// Input dimensions of the matrix

std::cout << "Enter number of rows: ";

std::cin >> m;

std::cout << "Enter number of columns: ";

std::cin >> n;

// Create an object of MyMatrix using dynamic constructor

MyMatrix myMatrix(m, n);

// Input elements into the matrix

myMatrix.inputMatrix();
// Display the sum of all elements

myMatrix.displaySum();

return 0;

Q3

#include <iostream>

class MyVector {

private:

int size; // Size of the vector

int* data; // Pointer to dynamically allocated array

public:

// Default constructor

MyVector() {

size=0 ,data=nullptr;

std::cout << "Default constructor called. Vector size is 0.\n";

// Parameterized constructor

MyVector(int s) {

size=s;

data = new int[size]; // Allocate memory for the vector

std::cout << "Parameterized constructor called. Vector size is " << size << ".\n";

for (int i = 0; i < size; ++i) {

data[i] = (i + 1) * 10; // Initialize with multiples of 10


}

// Destructor to free memory

~MyVector() {

delete[] data; // Deallocate memory

std::cout << "Destructor called. Memory freed.\n";

// Function to display the vector

void display() const {

if (data == nullptr) {

std::cout << "Vector is empty.\n";

return;

std::cout << "Vector elements: (";

for (int i = 0; i < size; ++i) {

std::cout << data[i];

if (i < size - 1) {

std::cout << ", ";

std::cout << ")\n";

};

int main() {

// Using default constructor


MyVector vec1; // Will be an empty vector

// Using parameterized constructor

int n;

std::cout << "Enter size of the vector: ";

std::cin >> n;

MyVector vec2(n); // Creates a vector of size n with initialized values

// Display the vectors

vec1.display(); // Display the empty vector

vec2.display(); // Display the initialized vector

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(int r, const std::string& n, int num) : rollNo(r), name(n), numSubjects(num) {

marks = new float[numSubjects]; // Dynamically allocate array for marks

// Destructor to free allocated memory

~Student() {

delete[] marks;

// Function to input marks

void inputMarks() {

std::cout << "Enter marks for " << numSubjects << " subjects: ";

for (int i = 0; i < numSubjects; i++) {

std::cin >> marks[i];

// Function to calculate percentage

float calculatePercentage() const {

float total = 0;

for (int i = 0; i < numSubjects; i++) {

total += marks[i];

return (total / numSubjects);

}
// Function to determine class obtained

std::string getClass() const {

float percentage = calculatePercentage();

if (percentage >= 60) {

return "First Class";

} else if (percentage >= 50) {

return "Second Class";

} else if (percentage >= 40) {

return "Third Class";

} else {

return "Fail";

// Function to display student details

void displayDetails() const {

std::cout << "\nRoll No: " << rollNo;

std::cout << "\nName: " << name;

std::cout << "\nNumber of Subjects: " << numSubjects;

std::cout << "\nMarks: ";

for (int i = 0; i < numSubjects; i++) {

std::cout << marks[i] << " ";

float percentage = calculatePercentage();

std::cout << "\nPercentage: " << std::fixed << std::setprecision(2) << percentage << "%";

std::cout << "\nClass Obtained: " << getClass() << "\n";

}
};

int main() {

int numStudents;

std::cout << "Enter number of students: ";

std::cin >> numStudents;

std::vector<Student> students;

// Input details for each student

for (int i = 0; i < numStudents; i++) {

int rollNo, numSubjects;

std::string name;

std::cout << "\nEnter details for student " << (i + 1) << ":\n";

std::cout << "Roll No: ";

std::cin >> rollNo;

std::cin.ignore(); // To ignore the newline character left in the buffer

std::cout << "Name: ";

std::getline(std::cin, name);

std::cout << "Number of Subjects: ";

std::cin >> numSubjects;

// Create student object

Student student(rollNo, name, numSubjects);

student.inputMarks();

students.push_back(student);

}
// Display details of all students

std::cout << "\nStudent Details:\n";

for (const auto& student : students) {

student.displayDetails();

return 0;

You might also like