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

Assignment 2

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

Assignment 2

Questions on C++
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Assignment no 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>

using namespace std;

// User: [Your Name], Reg. No.: [Your Registration Number]

struct Student {

string name;

int age;

char gender;

string address;

string phone_no;

int marks[3];

// Function to get data from the user

void get_data() {

cout << "Enter name: ";

getline(cin, name);

cout << "Enter age: ";

cin >> age;

cout << "Enter gender (M/F): ";

cin >> gender;

cin.ignore(); // to ignore the newline character after gender input

cout << "Enter address: ";


getline(cin, address);

cout << "Enter phone number: ";

getline(cin, phone_no);

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

cout << "Enter marks for subject " << i+1 << ": ";

cin >> marks[i];

cin.ignore(); // to ignore the newline character after marks input

// Function to display information

void display_information() const {

cout << "\nName: " << name << "\nAge: " << age << "\nGender: " << gender << "\nAddress: " <<
address << "\nPhone Number: " << phone_no;

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

cout << "\nMarks for subject " << i+1 << ": " << marks[i];

cout << endl;

};

int main() {

Student students[10];

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

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

students[i].get_data();

cout << "\nDisplaying student information:\n";

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

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>

using namespace std;

// User: [Your Name], Reg. No.: [Your Registration Number]

// Inline function to calculate cube root

inline double cubeRoot(double n) {

return cbrt(n); // Using built-in cbrt function

int main() {

double n;

cout << "Enter a number: ";

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

calculate the grade based on the marks.

#include <iostream>

#include <string>

using namespace std;

// User: [Your Name], Reg. No.: [Your Registration Number]

class Student {

private:

string name;

int rollNumber;

int marks;

char grade;

public:

void setData(string n, int r, int m) {

name = n;

rollNumber = r;

marks = m;

calculateGrade();

void calculateGrade() {

if (marks >= 90)

grade = 'A';

else if (marks >= 80)

grade = 'B';

else if (marks >= 70)


grade = 'C';

else if (marks >= 60)

grade = 'D';

else

grade = 'F';

void displayData() {

cout << "Name: " << name << "\nRoll Number: " << rollNumber << "\nMarks: " << marks << "\
nGrade: " << grade << endl;

};

int main() {

Student student;

student.setData("John Doe", 101, 85);

student.displayData();

return 0;

5. Discuss, how are data and functions organized in an object-oriented program?

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>

using namespace std;

// User: [Your Name], Reg. No.: [Your Registration Number]

// Volume of a cube

double volumeCube(double side) {

return side * side * side;

// Volume of a cylinder

double volumeCylinder(double radius, double height) {

return 3.14 * radius * radius * height;

// Volume of a rectangular prism

double volumeRectangularPrism(double length, double width, double height) {

return length * width * height;

int main() {

double side, radius, height, length, width;

cout << "Enter side of the cube: ";

cin >> side;

cout << "Volume of the cube: " << volumeCube(side) << endl;

cout << "\nEnter radius and height of the cylinder: ";

cin >> radius >> height;

cout << "Volume of the cylinder: " << volumeCylinder(radius, height) << endl;
cout << "\nEnter length, width, and height of the rectangular prism: ";

cin >> length >> width >> height;

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>

using namespace std;

// User: [Your Name], Reg. No.: [Your Registration Number]

class Book {

private:

string title;

string author;

string isbn;

public:

// Method to set book details

void setDetails(string t, string a, string i) {

title = t;

author = a;

isbn = i;

// Method to display book details


void displayDetails() {

cout << "Title: " << title << "\nAuthor: " << author << "\nISBN: " << isbn << endl;

};

int main() {

Book book;

book.setDetails("The Great Gatsby", "F. Scott Fitzgerald", "9780743273565");

book.displayDetails();

return 0;

You might also like