Assignment 2
Assignment 2
Kanishk Tomar
23BCE10588
1.Implement a program to show the information of 10 students using Structure concepts. Take data
members: name, age, gender, address, phone no, marks in three subjects.Function Used:Get_data(),
display_infromation()
#include <iostream>
#include <string>
struct Student {
string name;
int age;
char gender;
string address;
string phone_no;
int marks[3];
void get_data() {
getline(cin, name);
getline(cin, phone_no);
cout << "Enter marks for subject " << i+1 << ": ";
cout << "\nName: " << name << "\nAge: " << age << "\nGender: " << gender << "\nAddress: " <<
address << "\nPhone Number: " << phone_no;
cout << "\nMarks for subject " << i+1 << ": " << marks[i];
};
int main() {
Student students[10];
cout << "\nEnter details for student " << i+1 << ":\n";
students[i].get_data();
cout << "\nDetails for student " << i+1 << ":\n";
students[i].display_information();
return 0;
2. Design a class diagram for ATM (automated teller machines) management system. Analyse
required classes and their respective data and functions
3. Implement a program to calculate the cube root of any number n given by the user using the inline
approach. Discuss the reason when to use the inline function and when to not.
#include <iostream>
#include <cmath>
int main() {
double n;
cin >> n;
cout << "Cube root of " << n << " is " << cubeRoot(n) << endl;
return 0;
When to Use Inline Function: Inline functions are used when the function is small, and the
overhead of a function call would be more than the inline expansion. They are ideal for short,
frequently called functions.
When Not to Use Inline Function: Avoid inline functions for large or complex functions, as this can
increase code size (code bloat) and may lead to reduced performance due to cache misses.
4.Create a Student class with attributes like name, rollNumber, and marks. Implement methods to
#include <iostream>
#include <string>
class Student {
private:
string name;
int rollNumber;
int marks;
char grade;
public:
name = n;
rollNumber = r;
marks = m;
calculateGrade();
void calculateGrade() {
grade = 'A';
grade = 'B';
grade = 'D';
else
grade = 'F';
void displayData() {
cout << "Name: " << name << "\nRoll Number: " << rollNumber << "\nMarks: " << marks << "\
nGrade: " << grade << endl;
};
int main() {
Student student;
student.displayData();
return 0;
In object-oriented programming (OOP), data and functions are organized around objects. An object is
an instance of a class, which is a blueprint that defines the data and functions.
Data (Attributes/Properties): These are variables that hold the state of an object. For
example, in a Car class, attributes could include make, model, and year.
Functions (Methods/Behaviors): These are functions defined within a class that operates on
the data attributes. They define the behavior of the objects. For example, in the Car class,
methods could include start(), accelerate(), and stop().
This encapsulation of data and functions within a class ensures that the data is protected from
unauthorized access and can only be modified using the methods provided by the class.
6.Gurcharan is fascinated by how can we know the exact volume of some objects just by knowing
some of their dimensions but he does not actually know how it actually works. Can you help him find
the volume of a cube, a cylinder, and a Rectangular Prism? Take Pi=3.14
#include <iostream>
// Volume of a cube
// Volume of a cylinder
int main() {
cout << "Volume of the cube: " << volumeCube(side) << endl;
cout << "Volume of the cylinder: " << volumeCylinder(radius, height) << endl;
cout << "\nEnter length, width, and height of the rectangular prism: ";
cout << "Volume of the rectangular prism: " << volumeRectangularPrism(length, width, height) <<
endl;
return 0;
7.Define a Book class with attributes title, author, and isbn. Provide methods to set and display these
attributes..
#include <iostream>
#include <string>
class Book {
private:
string title;
string author;
string isbn;
public:
title = t;
author = a;
isbn = i;
cout << "Title: " << title << "\nAuthor: " << author << "\nISBN: " << isbn << endl;
};
int main() {
Book book;
book.displayDetails();
return 0;