0% found this document useful (0 votes)
23 views24 pages

Oops File

Uploaded by

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

Oops File

Uploaded by

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

INDEX

S.NO OBJECTIVE DATE REMARKS


1. Write a program to create a class, initialize and print 9/8/24
the data member of the class.
2. Write a program to implement a class called circle that 9/8/24
has private member variables for radius. Include
member functions to calculate the circle’s area and
circumference.
3. Write a program to demonstrate the use of structures in 16/8/24
C++.
4. Define a class to represent a bank account. 16/8/24
Include the following members-1) Type of account
2) Account Number 3) Balance amount 4) Name of
Depositor
5. Demonstrate passing of object to class function and 23/8/24
demonstrate its utility.
6. Create a class having integer A and B. Demonstrate the 23/8/24
use of friend function sum of class Complex that adds
the corresponding A’s and Bs of two objects and deploy
the sum.
7. Write a C++ program to perform different arithmetic 30/8/24
operation such as addition, subtraction, division, modulus
and multiplication using inline function.
8. WAP to return absolute value of variable types integer 30/8/24
and float using function overloading.
9. WAP to perform string operations using operator 6/9/24
overloading in C++ i. = String Copy ii. ==,>,< Equality
iii. + Concatenation
10. Consider a class network of figure given below. The 6/9/24
class master derives information from both account and
admin classes which in turn derive information from
the class person. Define all the four classes and write a
program to create, update and display the information
contained in master objects. Also demonstrate the use
of different access specifiers by means of member
variables and member functions.
11. Write a C++ program to create three objects for a class 13/9/24
named pntr_obj with data members such as roll_no and
name. Create a member function set_data() for setting the
data values and print() member function to print which
object has invoked it using ‘this’ pointer

12. Write a C++ program to explain virtual function 13/9/24


(polymorphism) by creating a base class c_polygon which
has virtual function area(). Two classes c_rectangle and
c_triangle derived from c_polygon and they have area() to
calculate and return the area of rectangle and triangle
respectively.
13. Write a program to explain class template by creating a 4/10/24
template T for a class named pair having two data members
of type T which are inputted by a constructor and a member
function get-max() return the greatest of two numbers to
main. Note: the value of T depends upon the data type
specified during object creation.
14. WAP to accept values from users, find sum, product, 4/10/24
difference, division of two numbers (a) using 3-5 inline
functions (b) using reference variable (c) using macros.
15. Write a program in C++ using static variable to get the sum 18/10/24
of the salary of 10 employees.
16. Write a program using a. natural function as friend b. 18/10/24
member function as friend to calculate the sum of two
complex numbers by using a class complex.
EXPERIMENT 1

AIM: Write a program to create a class, initialize and print the data member of the class.

CODE:

#include <iostream>

using namespace std;

class ok {
private:
int a = 0;

public:
void seta(int b) {
a = b;
}

int geta() {
return a;
}
};

int main() {
ok p;
p.seta(10);
cout << p.geta();
return 0;
}

OUTPUT:
EXPERIMENT 2
AIM: Write a program to implement a class called circle that has private member variables for
radius. Include member functions to calculate the circle’s area and circumference.

CODE:

#include <iostream>

using namespace std;

class ok {
private:
int a = 0;

public:
void seta(int b) {
a = b;
}

int geta() {
return a;
}

float area() {
return 3.14 * a * a;
}

float circumference() {
return 6.28 * a;
}
};

OUTPUT:
EXPERIMENT 3
AIM: Write a program to demonstrate the use of structures in C++.

CODE:
#include <iostream>
#include <string>

using namespace std;

struct student {
string name;
int roll;
char grade;
};

int main() {
student s[2];

s[0].name = "Jay";
s[0].roll = 1;
s[0].grade = 'A';

s[1].name = "Arun";
s[1].roll = 2;
s[1].grade = 'B';

cout << s[0].name << " " << s[0].roll << " " << s[0].grade <<
endl; cout << s[1].name << " " << s[1].roll << " " <<
s[1].grade << endl; return 0;
}

OUTPUT:
EXPERIMENT 4

AIM: Define a class to represent a bank account. Include the following members-
1) Type of account
2)Account Number
3) Balance amount
4) Name of Depositor

CODE:
#include <bits/stdc++.h>

using namespace std;

class account {
private: char
name[110];
int accountno;
float withdrawal;
float balance = 0;

public:
void setname() {
cout << "Enter the name : " <<
endl; cin >> name;
}

void setaccountno() {
cout << "Enter account number : " <<
endl; cin >> accountno;
}

void starting() {
cout << "Enter the initial amount" << endl;
if (balance ==
0) { cin >>
balance;
} else {
cout << "Error detected" << endl;
}
}

void depositamount() {
cout << "Enter amount to deposit : "
<< endl; int d; cin >> d;
balance += d;
cout << "New balance is : " << balance << endl;
}
void withdrawamount() {
cout << "Enter amount to withdraw : "
<< endl; int d; cin >> d;
balance -= d;
cout << "New balance is : " << balance << endl;
}

void showname() {
cout << "Name is : " << name << endl;
}

void showbalance() {
cout << "Balance is : " << balance << endl;
}
};

int main() {
account p;
p.setname();
cout << endl;

p.setaccountno();
cout << endl;
p.starting();
cout << endl;
p.depositamount();
cout << endl;
p.withdrawamount();
cout << endl;
p.showname();
p.showbalance();
return 0;
}

OUTPUT:
EXPERIMENT 5
AIM: Demonstrate passing of object to class function and demonstrate its utility.

CODE:
#include <bits/stdc++.h>

using namespace std;

class marks {
public:
int a = 0, b = 0;

void seta(int x) {
a = x;
}

void setb(int x) {
b = x;
}

int geta() {
return a;
}

int getb() {
return b;
}
};

marks sum(marks &x, marks &y) {


marks z;
z.a = x.a + y.a;
z.b = x.b + y.b; return z;
}

OUTPUT:
EXPERIMENT 6
AIM: Create a class having integer A and B. Demonstrate the use of friend function sum of class
Complex that adds the corresponding A’s and Bs of two objects and deploy the sum.

CODE:
#include <bits/stdc++.h>

using namespace std;

class Complex {
public:
float real, imag;

void setReal(float r) {
real = r;
}

void setImag(float i) {
imag = i;
}

float getReal() {
return real;
}

float getImag() {
return imag;
}
};

OUTPUT:
EXPERIMENT 7

AIM: Write a C++ program to perform different arithmetic operation such as addition,
subtraction, division, modulus and multiplication using inline function.

CODE:
#include <iostream>

using namespace std;

class operations {
public:
int i, j;

void get_data(int a,
int b) { i = a;
j = b;
}

void put_data() {
cout << "a = " << i <<
endl; cout << "b = " << j
<< endl;
}

inline int add() {


return i + j;
}

inline int subtract()


{ return i - j;
}

inline int multiply()


{ return i * j;
}

inline int divide()


{ return i / j;
}

inline int modulus() {


return i % j;
}
};
OUTPUT:
EXPERIMENT 8

AIM: WAP to return absolute value of variable types integer and float using function overloading.

CODE:
#include <iostream>

using namespace std;

int absolute(int m) {
return m >= 0 ? m : -m;
}

float absolute(float m) {
return m >= 0 ? m : -m;
}

int main() {
cout << "Enter the value of
integer - "; int x; cin >>
x;
cout << "The absolute value of " << x << " is = " << absolute(x) << endl;

cout << "Enter the value of


float - "; float y; cin >>
y;
cout << "The absolute value of " << y << " is = " << absolute(y) << endl;

return 0;
}

OUTPUT:
EXPERIMENT 9

AIM: WAP to perform string operations using operator overloading in C++


i. = String Copy
ii. ii. ==,>,< Equality iii. iii. + Concatenation

CODE:
#include <iostream>
#include <string> // Use `<string>` for string manipulation

using namespace std;

class String {
public:
string str;

String()
{ str
= "";
}

String(string
s) { str =
s;
}

// Overloaded assignment
operator (=) void
operator=(const String& s) {
cout << "Operator Overloaded =\n";
str = s.str;
}

// Overloaded addition operator (+) - returns a


new string String operator+(const String& s) {
cout << "Operator Overloaded +\n"; String
temp; temp.str = str + s.str; return
temp;
}
// Overloaded equality operator (==)
bool operator==(const String& s) {
cout << "Operator overloaded == " << endl;
return str == s.str;
}

// Overloaded less-than operator (<) - case-sensitive


comparison bool operator<(const String& s) {
cout << "Operator overloaded < " << endl; int len =
min(str.size(), s.str.size()); for (int i = 0; i <
len; i++) { if (str[i] < s[i]) {
return true; } else if (str[i] > s[i]) {
return false;
}
}
// If both strings are equal up to the shorter length,
// the longer string is considered "greater"
return str.size() < s.size();
}

// Overloaded greater-than operator (>) bool


operator>(const String& s) { return *this != s && !(*this
< s); // Use existing operators for efficiency
}

void show() {
cout << str << "\n";
}
};

OUTPUT:
EXPERIMENT 10
AIM: Consider a class network of figure given below. The class master derives information from
both account and admin classes which in turn derive information from the class person. Define all
the four classes and write a program to create, update and display the information contained in
master objects. Also demonstrate the use of different access specifiers by means of member
variables and member functions.

CODE:
#include <iostream>

using namespace std;

class person {
private:
string name;
int code;

public:
person(string s,
int c) { name
= s; code = c;
cout << "\nPerson constructor called";
}

void display();
void updateName(string);
void updateCode(int);
};

class account : virtual public


person { private: int pay;

public:
account(string s, int c, int p) :
person(s, c) { pay = p;
cout << "\nAccount constructor called";
}

void display();
void updatePay(int);
};

class admin : virtual public person {


private:
int experience;

public:
admin(string s, int c, int exp) : person(s, c) {
experience = exp;
cout << "\nAdmin constructor called";
}

void display();
void updateExp(int);
};

class master : public account, public admin {


public:
master(string s, int c, int p, int exp) : account(s, c, p), admin(s, c,
exp), person(s, c) { cout << "\nMaster constructor called";
}

void update();
void display();
};

OUTPUT:
EXPERIMENT 11
AIM: Write a C++ program to create three objects for a class named pntr_obj with data members
such as roll_no and name. Create a member function set_data() for setting the data values and
print() member function to print which object has invoked it using
‘this’ pointer

CODE:
#include <bits/stdc++.h>

using namespace std;

class prn_obj
{ public:
int rno;
string name;

void set_data(string n,
int r) { name = n;
rno = r;
}

void print() {
cout << this->name << " has invoked print() function" <<
endl; cout << "The roll number is " << this->rno <<
endl;
}
};

OUTPUT:
EXPERIMENT 12
AIM: Write a C++ program to explain virtual function (polymorphism) by creating a base class
c_polygon which has virtual function area(). Two classes c_rectangle and c_triangle derived
from c_polygon and they have area() to calculate and return the area of rectangle and triangle
respectively.

CODE:
#include <bits/stdc++.h>

using namespace std;

class c_polygon {
public:
virtual float area() = 0;
};

class c_triangle : public c_polygon {


private:
float a, b, c;

public:
c_triangle() {}

void setSides(float x, float y, float z) {


a =
x;
b = y;
c = z;
}

float area() override


{ float s = (a +
b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
};

class c_rectangle : public c_polygon {


private:
float l, b;

public:
c_rectangle() {}

void setSides(float x,
float y) { l = x;
b = y;
}
float area() override {
return l * b;
}
};

int main() {
cout << "Enter the sides of the triangle: ";
c_triangle tri;
float a, b, c;
cin >> a >> b >> c;
tri.setSides(a, b,
c);
cout << "Area of the triangle with sides " << a << ", " << b << ", " << c
<< " = " << tri.area() << endl;

cout << "Enter the sides of the


rectangle: "; c_rectangle rect;
float l, w; cin >> l >> w;
rect.setSides(l, w);
cout << "Area of the rectangle with sides " << l << ", " << w << " = " <<
rect.area() << endl;

return 0;
}

OUTPUT:
EXPERIMENT 13

AIM: Write a program to explain class template by creating a template T for a class named pair
having two data members of type T which are inputted by a constructor and a member function
get-max() return the greatest of two numbers to main. Note: the value of T depends upon the
data type specified during object creation.

CODE:

#include <iostream>

using namespace std;

template <class
T> class Pair {
public:
T a, b;

Pair(T x, T
y) { a
= x; b
= y;
}

T get_max() {
return max(a, b); // Using the standard max function
}
};

OUTPUT:
`

EXPERIMENT 14

AIM: WAP to accept values from users, find sum, product, difference, division of two numbers
(a) inline functions (b) using reference variable (c) using macros.

CODE:

#include <iostream>

using namespace std;

inline int add(int a, int b)


{ return a + b;
}

inline int subtract(int a, int


b) { return a - b;
}

inline int multiply(int a, int


b) { return a * b;
}

inline float divide(int a,


int b) { if (b == 0) {
cout << "Error: Division by zero\n";
return 0.0;
}
return (float)a / b;
}

void calculate(int a, int b, int& sum, int& difference, int& product,


float& division) { sum = a + b; difference = a - b; product =
a * b; if (b == 0) {
cout << "Error: Division by zero\n";
division = 0.0;
} else {
division = (float)a / b;
}
}

#define ADD(a, b) (a + b)
#define SUBTRACT(a, b) (a - b)
#define MULTIPLY(a, b) (a * b)
#define DIVIDE(a, b) ((b == 0) ? 0.0 : (float)a / b)
int main() {
int a, b, sum, difference, product;
float division;

cout << "Enter two integers:


"; cin >> a >> b;

cout << "Sum: " << add(a, b) << endl;


cout << "Difference: " << subtract(a, b) <<
endl; cout << "Product: " << multiply(a,
b) << endl;
cout << "Division: " << divide(a, b) << endl;

calculate(a, b, sum, difference, product,


division); cout << "Sum: " << sum << endl;
cout << "Difference: " << difference << endl;
cout << "Product: " << product << endl;
cout << "Division: " << division << endl;

cout << "Sum: " << ADD(a, b) << endl;


cout << "Difference: " << SUBTRACT(a, b) <<
endl; cout << "Product: " << MULTIPLY(a, b)
<< endl;
cout << "Division: " << DIVIDE(a, b) << endl;

return 0;
}

OUTPUT:
`

EXPERIMENT 15

AIM: Write a program in C++ using static variable to get the sum of the salary of 10
employees.

CODE:

#include <iostream>

using namespace std;

class Employee {
public:
int salary;
static int totalSalary;

void setSalary(int salary) {


this->salary = salary;
}

static void calculateTotalSalary(Employee employees[],


int n) { totalSalary = 0; for (int i = 0;
i < n; i++) {
totalSalary += employees[i].salary;
}
}

static void displayTotalSalary() {


cout << "Total salary of 10 employees: " << totalSalary << endl;
}
};

int Employee::totalSalary = 0;

OUTPUT:
EXPERIMENT 16

AIM: Write a program using a. natural function as friend b. member function as friend to
calculate the sum of two complex numbers by using a class complex.

CODE:

#include <iostream>

using namespace std;

class Complex {
public:
int real, imag;

Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}

// Friend function to add two Complex numbers


friend Complex add(Complex c1, Complex c2);

// Member function to add two Complex numbers


Complex add(Complex c2) {
Complex res; res.real
= real + c2.real;
res.imag = imag + c2.imag;
return res;
}
};

Complex add(Complex c1, Complex c2) {


Complex res; res.real
= c1.real + c2.real;
res.imag = c1.imag + c2.imag;
return res;
}

OUTPUT:

You might also like