0% found this document useful (0 votes)
2 views18 pages

Assignment 3-2

The document contains multiple programming assignments focused on C++ classes and their functionalities. Key topics include managing student information, calculating average heights, finding maximum and minimum values, handling product details, and performing arithmetic operations. Each assignment demonstrates concepts such as constructors, destructors, friend functions, and dynamic memory allocation.

Uploaded by

Hari Kalu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

Assignment 3-2

The document contains multiple programming assignments focused on C++ classes and their functionalities. Key topics include managing student information, calculating average heights, finding maximum and minimum values, handling product details, and performing arithmetic operations. Each assignment demonstrates concepts such as constructors, destructors, friend functions, and dynamic memory allocation.

Uploaded by

Hari Kalu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Assignment 3

SET A

Q1 Student Details

#include <iostream>

#include <string>

using namespace std;

class Student {

private:

int Roll_No;

string Student_Name;

string Class;

static int studentCount; // Static data member to count the number of students

public:

// Constructor to initialize student information

Student(int rollNo = 0, string name = "", string className = "")

: Roll_No(rollNo), Student_Name(name), Class(className) {

studentCount++; // Increment the count when a new student is created

// Destructor

~Student() {

studentCount--; // Decrement the count when a student object is destroyed

// Member function to accept student information

void acceptStudentInfo() {
cout << "Enter Roll Number: ";

cin >> Roll_No;

cin.ignore(); // To ignore the newline character left by cin

cout << "Enter Student Name: ";

getline(cin, Student_Name);

cout << "Enter Class: ";

getline(cin, Class);

// Member function to display student information

void displayStudentInfo() const {

cout << "Roll Number: " << Roll_No << endl;

cout << "Student Name: " << Student_Name << endl;

cout << "Class: " << Class << endl;

// Static member function to display the count of students

static void displayStudentCount() {

cout << "Total number of students: " << studentCount << endl;

};

// Initialize static data member

int Student::studentCount = 0;

int main() {

int numStudents;

// Ask user for the number of students


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

cin >> numStudents;

cin.ignore(); // To ignore the newline character left by cin

// Array of Student objects

Student* students = new Student[numStudents];

// Accept and display student information

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

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

students[i].acceptStudentInfo();

cout << "\nStudent Information:\n";

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

cout << "\nStudent " << (i + 1) << ":\n";

students[i].displayStudentInfo();

// Display total number of students

Student::displayStudentCount();

// Free allocated memory

delete[] students;

return 0;

}
Q2 Student Height

#include <iostream>

using namespace std;

class Student {

public:

double height; // Public member variable to store height

// Constructor to initialize height

Student(double h = 0.0) : height(h) {}

};

int main() {

int numStudents;

// Ask user for the number of students

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

cin >> numStudents;

// Ensure the number of students is positive

if (numStudents <= 0) {

cout << "The number of students must be greater than zero." << endl;

return 1; // Exit with an error code

// Create an array of Student objects

Student* students = new Student[numStudents];

// Input heights for each student


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

cout << "Enter height for student " << (i + 1) << ": ";

cin >> students[i].height;

// Calculate the average height

double totalHeight = 0.0;

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

totalHeight += students[i].height;

double averageHeight = totalHeight / numStudents;

// Output the average height

cout << "The average height of the students is: " << averageHeight << endl;

// Free allocated memory

delete[] students;

return 0;

Q3 Maximum And Minimum

#include<iostream>

using namespace std;

void findMaximum(); //function prototype

void findMinimum();
class Number1

int num1;

public:

Number1()

cout<<"please enter num1:";

cin>>num1;

friend void findMaximum(); //friend function

friend void findMinimum();

};

class Number2

int num2;

public:

Number2()

cout<<"please enter num2:";

cin>>num2;

friend void findMaximum(); //friend function

friend void findMinimum();


};

void findMaximum()

Number1 n1;

Number2 n2;

if (n1.num1 > n2.num2)

cout<<"\nnum1 is maximum"<<endl;

else

cout<<"\nnum2 is maximum"<<endl;

void findMinimum()

Number1 n1;

Number2 n2;

if (n1.num1 < n2.num2)

cout<<"\nnum1 is minimum"<<endl;

else

cout<<"\nnum2 is minimum"<<endl;

int main()

findMaximum();

findMinimum();

return 0;

}
SET B

Q1 Product

#include <iostream>

#include <string>

using namespace std;

class Product {

private:

string name;

float price;

public:

// Method to input product details

void input() {

cout << "Enter product name: ";

getline(cin, name);

cout << "Enter product price: ";

cin >> price;

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

// Method to display product details

void display() const {

cout << "Product Name: " << name << endl;

cout << "Product Price: " << price << endl;

}
// Method to get the price of the product

float getPrice() const {

return price;

};

int main() {

int n;

cout << "Enter the number of products: ";

cin >> n;

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

// Dynamically allocate memory for array of Product objects

Product* products = new Product[n];

// Input product information

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

cout << "Enter details for product " << (i + 1) << endl;

products[i].input();

// Display all product information

cout << "\nProduct Information:\n";

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

cout << "Product " << (i + 1) << ":\n";

products[i].display();

cout << endl;


}

// Find and display the product with the maximum price

int maxIndex = 0;

for (int i = 1; i < n; ++i) {

if (products[i].getPrice() > products[maxIndex].getPrice()) {

maxIndex = i;

cout << "Product with maximum price:\n";

products[maxIndex].display();

// Deallocate memory

delete[] products;

return 0;

Q2 Feet and Inches

#include <iostream>

using namespace std;

class Distance {

private:

int feet;

int inches;
public:

// Function to accept distance

void acceptDistance() {

cout << "Enter feet: ";

cin >> feet;

cout << "Enter inches: ";

cin >> inches;

// Convert inches to feet if more than 12

if (inches >= 12) {

feet += inches / 12;

inches = inches % 12;

// Function to display distance

void displayDistance() const {

cout << feet << " feet " << inches << " inches" << endl;

// Function to add two Distance objects

Distance add(const Distance& d) const {

Distance temp;

temp.inches = inches + d.inches;

temp.feet = feet + d.feet + temp.inches / 12;

temp.inches = temp.inches % 12;

return temp;

};
int main() {

Distance d1, d2, result;

cout << "Enter details for first distance:\n";

d1.acceptDistance();

cout << "Enter details for second distance:\n";

d2.acceptDistance();

// Display distances

cout << "First distance: ";

d1.displayDistance();

cout << "Second distance: ";

d2.displayDistance();

// Add distances

result = d1.add(d2);

// Display result of addition

cout << "Sum of distances: ";

result.displayDistance();

return 0;

Q3

#include <iostream>
using namespace std;

class Array2; // Forward declaration of Array2

class Array1 {

private:

int arr[5]; // Array of size 5

int size; // Size of the array

public:

// Constructor

//Array1() : size(0) {}

// Function to accept array elements

void acceptArray() {

cout << "Enter 5 elements for Array1:" << endl;

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

cin >> arr[i];

size = 5;

// Function to display array elements

void displayArray() const {

cout << "Array1 elements: ";

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

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

cout << endl;


}

// Friend function declaration

friend void findAndDisplayMax(const Array1&, const Array2&);

};

class Array2 {

private:

int arr[5]; // Array of size 5

int size; // Size of the array

public:

// Constructor

// Array2() : size(0) {}

// Function to accept array elements

void acceptArray() {

cout << "Enter 5 elements for Array2:" << endl;

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

cin >> arr[i];

size = 5;

// Function to display array elements

void displayArray() const {

cout << "Array2 elements: ";

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

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


}

cout << endl;

// Friend function declaration

friend void findAndDisplayMax(const Array1&, const Array2&);

};

// Friend function definition

void findAndDisplayMax(const Array1& a1, const Array2& a2) {

int maxArray1 = a1.arr[0];

int maxArray2 = a2.arr[0];

// Find max in Array1

for (int i = 1; i < 5; ++i) {

if (a1.arr[i] > maxArray1) {

maxArray1 = a1.arr[i];

// Find max in Array2

for (int i = 1; i < 5; ++i) {

if (a2.arr[i] > maxArray2) {

maxArray2 = a2.arr[i];

// Display maximum values

cout << "Maximum in Array1: " << maxArray1 << endl;


cout << "Maximum in Array2: " << maxArray2 << endl;

int main() {

Array1 array1;

Array2 array2;

// Accept and display array elements

array1.acceptArray();

array1.displayArray();

array2.acceptArray();

array2.displayArray();

// Find and display maximum values

findAndDisplayMax(array1, array2);

return 0;

SET C

Q1 Multiplication

#include<iostream>

using namespace std;

void multiplication(); //function prototype


class Number1

int num1;

public:

Number1()

cout<<"please enter num1:";

cin>>num1;

friend void multiplication(); //friend function

};

class Number2

int num2;

public:

Number2()

cout<<"please enter num2:";

cin>>num2;

friend void multiplication(); //friend function


};

void multiplication()

Number1 n1;

Number2 n2;

int product = n1.num1 * n2.num2;

cout << "Multiplication of " << n1.num1 << " and " << n2.num2 << " is: " << product << endl;

int main()

multiplication();

return 0;

You might also like