0% found this document useful (0 votes)
21 views8 pages

Assignment 3

The document contains a series of C++ programming assignments focused on various concepts such as class objects, pointers, member functions, inline functions, and basic arithmetic operations. Each assignment includes code snippets for entering and displaying book and student records, comparing numbers, calculating sums, and simulating an arithmetic calculator. The programs demonstrate fundamental programming techniques and object-oriented principles in C++.

Uploaded by

Ujjwal Deep Kaur
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)
21 views8 pages

Assignment 3

The document contains a series of C++ programming assignments focused on various concepts such as class objects, pointers, member functions, inline functions, and basic arithmetic operations. Each assignment includes code snippets for entering and displaying book and student records, comparing numbers, calculating sums, and simulating an arithmetic calculator. The programs demonstrate fundamental programming techniques and object-oriented principles in C++.

Uploaded by

Ujjwal Deep Kaur
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

Assignment C++ Programs (Lab -3)

Q1. To enter required number of book records and display them using array
declaration of class’s objects .
#include <iostream>
#include <string>
using namespace std;

class Book {
private:
string title;
string author;
int year;

public:
void inputDetails() {
cout << "Enter book title: ";
getline(cin, title);
cout << "Enter book author: ";
getline(cin, author);
cout << "Enter publication year: ";
cin >> year;
cin.ignore();
}
void displayDetails() const {
cout << "Title: " << title << endl;
cout << "Author: " << author << endl;
cout << "Year: " << year << endl;
}
};

int main() {
int numBooks;
cout << "Enter the number of books: ";
cin >> numBooks;
cin.ignore();
Book* books = new Book[numBooks];

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


cout << "\nEnter details for book " << (i + 1) << ":\n";
books[i].inputDetails();
}
cout << "\nDisplaying book details:\n";
for (int i = 0; i < numBooks; ++i) {
cout << "\nBook " << (i + 1) << " details:\n";
books[i].displayDetails();
}
delete[] books;

return 0;
}
Q2. Program To enter the data of a students and display it using indirection
operator(using pointer to class objects) and structure operator.
#include <iostream>
#include <string>
using namespace std;

class Student {
private:
string name;
int rollNumber;
char grade;

public:
void inputDetails() {
cout << "Enter student name: ";
getline(cin, name);
cout << "Enter roll number: ";
cin >> rollNumber;
cout << "Enter grade: ";
cin >> grade;
cin.ignore();
}
void displayDetails() const {
cout << "Name: " << name << endl;
cout << "Roll Number: " << rollNumber << endl;
cout << "Grade: " << grade << endl;
}
string getName() const { return name; }
int getRollNumber() const { return rollNumber; }
char getGrade() const { return grade; }
};

int main() {
int numStudents;
cout << "Enter the number of students: ";
cin >> numStudents;
cin.ignore();
Student* students = new Student[numStudents];

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


cout << "\nEnter details for student " << (i + 1) << ":\n";
students[i].inputDetails();
}
cout << "\nDisplaying student details:\n";
for (int i = 0; i < numStudents; ++i) {
cout << "\nStudent " << (i + 1) << " details:\n";
students[i].displayDetails();
}

return 0;
}

Q3. Program To find the highest of two numbers using the nesting of member
functions.
#include <iostream>
using namespace std;

class NumberComparator {
private:
int num1, num2;
int findMax() {
return (num1 > num2) ? num1 : num2;
}
public:
void inputNumbers() {
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
}
void displayHighest() {
int maxNum = findMax();
cout << "The highest number is: " << maxNum << endl;
}
};
int main() {
NumberComparator comparator;
comparator.inputNumbers();
comparator.displayHighest();
return 0;
}
Q4. Program using of inline function sum of three numbers.
#include <iostream>
using namespace std;

class Calculator {
public:
inline int sum(int a, int b, int c) {
return a + b + c;
}
};
int main() {
Calculator calc;
int num1, num2, num3;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
cout << "Enter the third number: ";
cin >> num3;
int result = calc.sum(num1, num2, num3);
cout << "The sum of the three numbers is: " << result << endl;
return 0;
}

Q5. Program to simulate an arithmetic calculator for integer. The program


should be able to produce the result calculated and the number of arithmetic
operators performed so far. Any wrong operation is to be reported.

You might also like