0% found this document useful (0 votes)
9 views28 pages

OOPS Aniket

The document contains multiple lab exercises in C++ focusing on various programming concepts such as account management, book issuing system, student management, and complex number operations. Each exercise includes code snippets demonstrating the implementation of classes, functions, and user interactions. The exercises cover topics like data structures, object-oriented programming, and operator overloading.
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)
9 views28 pages

OOPS Aniket

The document contains multiple lab exercises in C++ focusing on various programming concepts such as account management, book issuing system, student management, and complex number operations. Each exercise includes code snippets demonstrating the implementation of classes, functions, and user interactions. The exercises cover topics like data structures, object-oriented programming, and operator overloading.
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/ 28

Name : Aniket Katiyar

Scholar No. : 24U024013


Branch : CSE (DS)

LAB EXERCISE 1

#include <bits/stdc+
+.h> using
namespace std;
struct account{
string
name; int
number ;
int
balance ;
};

void deposit(vector<account>
acc){ int accnum;
cout << "Account
Number - "; cin >>
accnum;
for(int i = 0;i < acc.size();i++){
if(acc[i].number ==
accnum){ cout <<
"Amount deposit - " ;
int am;
cin >> am;
acc[i].balance += am;
cout << "Name - " << acc[i].name
<< endl; cout << "Number - " <<
acc[i].number << endl;
cout << "Current balance - " << acc[i].balance << endl << endl;
}
}
}
void
withdraw(vector<account>
acc){ int accnum;
cout << "Account
Number - " ; cin >>
accnum;
for(int i = 0;i < acc.size();i++){
if(acc[i].number ==
accnum){ int am;
cout << "withdraw
amount - "; cin >> am;
if(am <= acc[i].balance)
acc[i].balance -= am; else cout <<
"Not enough balance" << endl; cout
<< "Name - " << acc[i].name <<
endl; cout << "Number - " <<
acc[i].number << endl;
cout << "Current balance - " << acc[i].balance << endl << endl;
}
}
}
void display(vector<account>
acc){ int accnum;
cout << "Account
Number - " ; cin >>
accnum;
for(int i = 0;i < acc.size();i++){
if(acc[i].number == accnum){

cout << "Name - " << acc[i].name


<< endl; cout << "Number - " <<
acc[i].number << endl;
cout << "Current balance - " << acc[i].balance << endl << endl;
}
}
}
void add_new_acc(vector<account> acc) {
string name;
cout << "Enter
Name - "; cin >>
name;
int
number;
bool k =
1;
while(k){
cout << "Enter
Number - "; cin >>
number;
for(int i = 0;i < acc.size();i++){
if(number == acc[i].number) cout << "Account number already
taken, try again\n"; else if(i == acc.size() - 1) k = 0;

}
}
acc.push_back({name,number
,0}); cout << "Account
added!\n";
}

int main(){
cout << "Aniket Katiyar\n24U024013\n\n";

vector<account> acc = {
{"Dakshesh",01,1500},
{"Shrey",02,2000},
{"Jayashish",03,5000},
{"Keshkanti",04,10000},
};
int
choice;
do{
cout << "\nEnter choices: \n1. deposit\n2. withdraw \n3. display\n4.
Add new account\n5. Exit\n\n";
cin >>
choice;
switch(choic
e){
case 1:
deposit(acc);
break;
case 2:
withdraw(acc);
break;
case 3:
display(acc);
break;
case 4:
add_new_acc(acc);
break;
case 5: exit;
default: break;
}
}while(choice != 5);
Return 0;
}
OUTPUT
LAB EXERCISE 2
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Book {
int bookID;
string title;
string authorName;
bool isIssued;
string issuedTo;
public :
void setBookDetails(int id, string bookt, string au) {
bookID = id;
title = bookt;
authorName = au;
isIssued = false;
issuedTo = "";
}

void addBook();
void issueBook(string name);
void returnBook();
void displayDetails();
int getBookID() {
return bookID;
}
};
void Book::addBook() {
cout << "Enter Book ID: ";
cin >> bookID;
cout << "Enter Title: ";
cin >> title;
cout << "Enter Author Name: ";
cin >> authorName;
cout << "Book added\n";
this->isIssued = false;
}
void Book::issueBook(string name) {
if (!this->isIssued) {
this->isIssued = true;
this->issuedTo = name;
cout << "Book issued to " << name << "." << endl;
} else {
cout << "Book is already issued to " << this->issuedTo << "." <<
endl;
}
}
void Book::returnBook() {
if (isIssued) {
isIssued = false;
issuedTo = "";
cout << "Book returned." << endl;
} else {
cout << "Book is not issued yet." << endl;
}
}
void Book::displayDetails() {
cout << "Book ID - " << bookID << endl;
cout << "Title - " << title << endl;
cout << "Author - " << authorName << endl;
cout << "Status - " << (isIssued ? "Issued" : "Not Issued") << endl;
if (isIssued) {
cout << "Issued To - " << issuedTo << endl;
}
}

int main() {
cout<<"Enter maximum number of books possible\n";
int n;
cin>>n;
Book books[n];
int c = 0;
int ch;
cout << "1. Add Book\n";
cout << "2. Issue Book\n";
cout << "3. Return Book\n";
cout << "4. Display Book Details\n";
cout << "5. Exit\n";
do {
cout << "Enter your choice: ";
cin >> ch;
switch (ch) {
case 1:
if (c < n) {
books[c].addBook();
c++;
} else {
cout << "Maximum book limit reached." << endl;
}
break;

case 2: {
int id;
string name;
cout << "Enter Book ID to issue: ";
cin >> id;
cout << "Enter person's name: ";
cin >> name;
bool f = false;
for (int i = 0; i < c; i++) {
if (id == books[i].getBookID()) {
books[i].issueBook(name);
f = true;
break;
}
}
if (!f)
cout << "Book not found." << endl;
break;
}

case 3: {
int id;
cout << "Enter Book ID to return: ";
cin >> id;
bool f = false;
for (int i = 0; i < c; i++) {
if (id == books[i].getBookID()) {
books[i].returnBook();
f = true;
break;
}
}
if (!f) {
cout << "Book not found." << endl;
}
break;
}

case 4: {
int id;
bool f = false;
for (int i = 0; i < c; i++) {
books[i].displayDetails();
}
break;
}

case 5:
cout << "Exiting" << endl;
break;

default:
cout << "Invalid choice" << endl;
}
} while (ch != 5);

return 0;
}

OUTPUT
LAB EXERCISE 3
#include <iostream>
using namespace std;
class student {
public:
string name;
string roll;
float marks;
static int studentCount;
void setStudentDetails(string name, string rollno, float marks) {
this->name=name;
this->roll=rollno;
this->marks=marks;
student::studentCount++;
}
void display () {
cout<<"Name: "<<name<<endl;
cout<<"Roll No: "<<roll<<endl;
cout<<"Marks: "<<marks<<endl;
}
static void showStudentCount() {
cout<<"Total Students: "<<student::studentCount<<endl;
}
void compare(student s) {
cout<<"Marks compared to other student"<<endl;
if (this->marks>s.marks) {
cout<<"This student has higher marks by "<<this->marks-
s.marks<<endl;
}
else if (s.marks>this->marks) {
cout<<"Other student has higher marks by "<<s.marks-this-
>marks<<endl;
}
else {
cout<<"Both students have same marks"<<endl;
}
}
};
int student::studentCount=0;
int main() {
cout<<"Enter number of students\n";
int n;
cin>>n;
student arr[n];
string name, roll;
float marks;
for (int i=0;i<n;i++) {
cout<<"Enter name, roll no and marks of student "<<i+1<<endl;
cin>>name>>roll>>marks;
arr[i].setStudentDetails(name, roll, marks);
}
cout<<"Enter your Roll no. to display details\n";
string rolln1;
cin>>rolln1;
int j;
int c=0;
for (int i=0;i<n;i++) {
if (arr[i].roll==rolln1) {
arr[i].display();
j=i;
c++;
}
}
if (c==0)
cout<<"Student not found\n";
cout<<"Enter Roll no. of other student to compare marks\n";
string rolln2;
cin>>rolln2;
int k;
c=0;
for (int i=0;i<n;i++) {
if (arr[i].roll==rolln2) {
k=i;
c++;
}
}
if (c==0)
cout<<"Student not found\n";
arr[j].compare(arr[k]);
student::showStudentCount();

OUTPUT
LAB EXERCISE 4
#include <iostream>
using namespace std;
class Student {
public:
int rollNumber;
string name;
int marks[3];
int totalMarks;
float averageMarks;
static int CountGrade[4];
static int objectNumber;
Student(){};
Student(int rnum, string n, int a, int b, int c) {
rollNumber=rnum;
name=n;
marks[0]=a;
marks[1]=b;
marks[2]=c;
totalMarks=a+b+c;
averageMarks=totalMarks/3;
}
char getGrade() {
if (averageMarks>=90) {
CountGrade[0]++;
return 'A';
}
else if (averageMarks>=75) {
CountGrade[1]++;
return 'B';
}
else if (averageMarks>=50) {
CountGrade[2]++;
return 'C';
}
else {
CountGrade[3]++;
return 'F';
}
}
void Display() {
cout<<"Roll Number : "<<rollNumber<<endl;
cout<<"Name : "<<name<<endl;
cout<<"Total Marks : "<<totalMarks<<endl;
cout<<"Average Marks : "<<averageMarks<<endl;
}
static void getStudentCount() {
char ch='A';
for (int i=0;i<3;i++)
cout<<"Number of students with grade "<<(ch++)<<" :
"<<CountGrade[i]<<endl;
cout<<"Number of students with grade "<<'F'<<" :
"<<CountGrade[3]<<endl;
}
~Student(){
objectNumber++;
}
};
int Student::CountGrade[]={0,0,0,0};
int Student::objectNumber=0;
int main() {
cout<<"Enter maximum number of students:";
int n;
cin>>n;
Student array[n];
int rolln;
string name;
int marks[3];
for (int i=0;i<n;i++) {
cout<<"Enter your Roll Number: ";
cin>>rolln;
cout<<"Enter your Name: ";
cin>>name;
cout<<"Enter your marks in each subject: ";
for (int j=0;j<3;j++)
cin>>marks[j];
Student s(rolln, name, marks[0], marks[1], marks[2]);
array[i]=s;
array[i].getGrade();
}
cout<<"Details of all students\n";
for (int i=0;i<n;i++) {
array[i].Display();
}
cout<<"Number of students of each grade\n";
array[0].getStudentCount();
}

OUTPUT
LAB EXERCISE 5
#include<iostream>
using namespace std;
class Student{
public:
string name;
int age;
float marks[3];
Student(){
name="unknown";
age=0;
marks[0]=marks[1]=marks[2]=0.0;
}
Student(string n, int a, float m[]){
name=n;
age=a;
marks[0]=m[0];
marks[1]=m[1];
marks[2]=m[2];
}
Student(Student &s){
name=s.name;
age=s.age;
marks[0]=s.marks[0];
marks[1]=s.marks[1];
marks[2]=s.marks[2];
}
void display(){
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Marks: "<<marks[0]<<" "<<marks[1]<<" "<<marks[2]<<"
"<<endl;
}
~Student(){
cout<<"Data destructed"<<endl;
}
};

int main(){
cout<<"Enter number of students"<<endl;
int n=3;
Student s[n];
float m1[3]={60.0, 70.0, 80.0};
s[0]={"Aniket",18,m1};
float m2[3]={55.0,60.0,65.0};
s[1]={"Sanchit",19,m2};
float m3[3]={85.0,90.0,95.0};
s[2]={"Shreyash",19,m3};
cout<<"Default contructor\n";
for(int i=0;i<n;i++) {
s[i].display();
}
Student s1("Aniket",18,m1);
Student s2("Sanchit",19,m2);
Student s3("Shreyash",19,m3);
cout<<"Parameterized contructor\n";
s1.display();
s2.display();
s3.display();
Student s4(s1);
cout<<"Copy Contructor\n";
s4.display();

} OUTPUT
LAB EXERCISE 6
# #include
<iostream>
using namespace
std;
class Complex {
int real;
int imag;
int im;
public :
Complex(int r, int
i) {
real=r;
imag=i;
}
Complex operator
+ (Complex &c) {
int
r=real+c.real;
int
i=imag+c.imag;
return
Complex(r, i);
}
Complex operator
- (Complex &c) {
int r=real-
c.real;
int i=imag-
c.imag;
return
Complex(r, i);
}
Complex operator
* (Complex &c) {
int
r=(real*c.real) -
(imag*c.imag);
int
i=(real*c.imag) +
(imag*c.real);
return
Complex(r, i);
}
Complex operator
% (Complex &c) {
int r=real
%c.real;
int i=imag
%c.imag;
return
Complex(r, i);
}
bool operator ==
(Complex &c) {
if(real==c.real
&& imag==c.imag)
return true;
else
return false;
}
void display () {
cout<<"Real
part =
"<<real<<endl;

cout<<"Imaginary
part =
"<<imag<<endl;
}
};
int main() {
cout<<"Enter
real and imaginary
part of first complex
number: \n";
int r1, i1;
cin>>r1>>i1;
cout<<"Enter
real and imaginary
part of second
complex number: \
n";
int r2, i2;
cin>>r2>>i2;
Complex c1(r1,
i1);
Complex c2(r2,
i2);
Complex
c3=c1+c2;
Complex c4=c1-
c2;
Complex
c5=c1*c2;
Complex
c6=c1%c2;
cout<<"Addition
of two complex
numbers: "<<endl;
c3.display();

cout<<"Subtraction
of two complex
numbers: "<<endl;
c4.display();
cout<<"Multiplicati
on of two complex
numbers: "<<endl;
c5.display();

cout<<"Remainder
of two complex
numbers: "<<endl;
c6.display();
if (c1==c2)
cout<<"Both
complex numbers
are equal"<<endl;
else
cout<<"Both
complex numbers
are not
equal"<<endl;
}

OUTPUT
LAB EXERCISE 7
#include
<iostream> using
namespace std;

class
person{ pr
otected:
string
name; int
userid;
public:
void display(){
cout << "Name - " << name << endl;
cout << "UserID - " << userid << endl << endl;
}
};
class student: public
person{ string
bookborrowed;
int
daysborrowed;
public:
student(){}
student(string n, int id, string bookbrw,
int days){ name = n;
userid = id;
bookborrowed =
bookbrw;
daysborrowed =
days;
}
void return_book(){
cout << "Book " << bookborrowed << " is submitted\n";
if(daysborrowed <= 7) cout << "Fine = " << 0 <<
endl << endl; else{
cout << "Fine = " << (daysborrowed - 7) * 2 << endl << endl;
}
name =
"";
userid =
0;
bookborrowed = "";
daysborrowed = 0;
}
};

int main(){
cout << "ANIKET KATIYAR\n24U024013\n\n";
string
name; int
id;
string
book; int
days;
cout << "Enter
Name - "; cin >>
name;
cout << "Enter User-
ID - "; cin >> id;
cout << "Enter Book
name - "; cin >> book;
cout << "Enter
days - "; cin >>
days;
cout << endl;
student A(name, id, book, days);
cout << "DATA BEFORE RETURNING THE BOOK - \n";
A.display();
cout << "RETURNING THE BOOK - \n";
A.return_book();
cout << "DATA AFTER RETURNING THE BOOK - \n";
A.display();
}

OUTPUT
LAB EXERCISE 8
#include
<iostream> using
namespace std;

class complex;
class
operations{
public:
friend complex add(const complex s,const
complex s); friend complex subtract(const
complex s,const complex s); friend complex
multiply(const complex s, const complex s);
};
class
complex{ in
t a, b;
public:
complex(
){}
complex(int r,int i){
a = r;
b = i;
}
void show(){
cout << a << " + " << b << "i" << endl;
}

friend complex add(const complex s,const


complex s); friend complex subtract(const
complex s,const complex s); friend complex
multiply(const complex s,const complex s);
};
complex add(const complexs c1, const
complexs c2) { return complex(c1.a + c2.a,
c1.b + c2.b);
}
complex subtract(const complexs c1, const
complexs c2) { return complex(c1.a - c2.a, c1.b -
c2.b);
}
complex multiply(const complexs c1, const
complexs c2) { float realPart = c1.a * c2.a - c1.b
* c2.b;
float imgPart = c1.a * c2.b + c1.b
* c2.a; return complex(realPart,
imgPart);
}
int main() {
cout << "ANIKET KATIYAR\n24U024013\n\n";
int a,b;
cout << "Enter first complex number -";
cin >> a >> b; // Input for first complex
number complex c1(a,b); // First
complex number
int c,d;
cout << "Enter first complex number -";
cin >> c >> d; // Input for second
complex number complex c2(c,d); //
Second complex number
cout << endl;
cout << "First complex number - " ;
c1.show(); // Display first complex
number cout << "Second complex
number - ";
c2.show(); // Display second complex number
cout << endl;
cout << "Operations - "
<< endl; complex sum =
add(c1,c2); complex diff
= subtract(c1,c2);
complex prod =
multiply(c1,c2); cout <<
"Sum - ";
sum.show(); // Display sum of complex
numbers cout << "Difference - ";
diff.show(); // Display difference of complex
numbers cout << "Product - ";
prod.show(); // Display product of complex
numbers cout << endl;
return 0;
}

OUTPUT
LAB EXERCISE 9A
#include
<iostream> using
namespace std;

class
A{ p
ublic:
void display(){
cout << "Hello!, from A" << endl;
}
};
class B: virtual public
A{ protected: int
val;
};
class C: virtual public
A{ protected: int
val;
};
class D: virtual public B,virtual
public C{ public:
void intialize(int
value){ B::val =
value;
C::val = value+10;
}
void displayval(){
cout << "Value of B - " <<
B::val << endl; cout << "Value
of C - " << C::val << endl;
}
};
int main() {
cout << "ANIKET KATIYAR\n24U024013\n\n";
D obj;
obj.intialize(10);
obj.display();
obj.displayval()
; return 0;
}

OUTPUT
LAB EXERCISE 9B

#include<bits/stdc++.h>
using namespace std;
class person
{
protected:
string name;
int age;
public:
person(string n,int a)
{
name=n;
age=a;
}
void display()
{
cout<<"NAME :"<<name<<endl;
cout<<"AGE :"<<age<<endl;
}
};
class student:virtual public person{
protected:
string rollno;
string course;
public:
student(string n,int a,string r,string c):person(n,a){
rollno=r;
course=c;
}
void display()
{
cout<<"ROLL NO. :"<<rollno<<endl;
cout<<"COURSE:"<<course<<endl;
}
};
class employee:virtual public person{
protected:
string empid;
double dept;
public:
employee(string n,int a,string emp,double dep):person(n,a){
empid=emp;
dept=dep;
}
void display()
{
cout<<"EMPLOYEE ID :"<<empid<<endl;
cout<<"DEPT :"<<dept<<endl;
}
};
class intern:public student,public employee{
public:
intern(string n,int a,string r,string c,string emp,double
dep):person(n,a),student(n,a,r,c),employee(n,a,emp,dep)
{

}
void display()
{
person::display();
student::display();
employee::display();
}
};
int main()
{
intern i("ANIKET",19,"24U024013","CSE(DATA
SCIENCE)","EMP16",20000.0);
i.display();

OUTPUT
LAB EXERCISE 10

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
class shape {
public:
virtual double area() = 0;
virtual double perimeter() = 0;
virtual void display() = 0;
};
class circle : public shape {
double r;
public :
circle(double ra) {
r=ra;
}
double area () {
return M_PI*r*r;
}
double perimeter () {
return 2*M_PI*r;
}
void display() {
cout<<"Circle Area = "<<area()<<endl;;
cout<<"Circle Perimeter = "<<perimeter()<<endl;
}
};
class rectangle : public shape {
double l,w;
public :
rectangle(double le, double wi) {
l=le;
w=wi;
}
double area () {
return l*w;
}
double perimeter () {
return 2*(l+w);
}
void display() {
cout<<"Rectangle Area = "<<area()<<endl;;
cout<<"Rectangle Perimeter = "<<perimeter()<<endl;
}
};
class triangle : public shape {
double a,b,c;
public :
triangle(double x, double y, double z) {
a=x;
b=y;
c=z;
}
double area () {
double s=(a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
double perimeter () {
return a+b+c;
}
void display() {
cout<<"Triangle Area = "<<area()<<endl;;
cout<<"Triangle Perimeter = "<<perimeter()<<endl;
}
};
int main() {
vector<shape*> s;
cout<<"Enter radius of the circle"<<endl;
int r;
cin>>r;
s.push_back(new circle(r));
cout<<"Enter length and width of the rectangle"<<endl;
int l,w;
cin>>l>>w;
s.push_back(new rectangle(l,w));
cout<<"Enter length of sides of the triangle"<<endl;
int a,b,c;
cin>>a>>b>>c;
s.push_back(new triangle(a,b,c));
sort(s.begin(), s.end(), [](shape* a, shape* b){
return a->area() > b->area();
});
for (auto s : s) s->display();
}

OUTPUT

You might also like