Oops File
Oops File
AIM: Write a program to create a class, initialize and print the data member of the class.
CODE:
#include <iostream>
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>
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>
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>
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>
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;
}
};
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>
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>
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;
}
AIM: WAP to return absolute value of variable types integer and float using function overloading.
CODE:
#include <iostream>
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;
return 0;
}
OUTPUT:
EXPERIMENT 9
CODE:
#include <iostream>
#include <string> // Use `<string>` for string manipulation
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;
}
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>
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);
};
public:
account(string s, int c, int p) :
person(s, c) { pay = p;
cout << "\nAccount constructor called";
}
void display();
void updatePay(int);
};
public:
admin(string s, int c, int exp) : person(s, c) {
experience = exp;
cout << "\nAdmin constructor called";
}
void display();
void updateExp(int);
};
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>
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>
class c_polygon {
public:
virtual float area() = 0;
};
public:
c_triangle() {}
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;
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>
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>
#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;
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>
class Employee {
public:
int salary;
static int totalSalary;
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>
class Complex {
public:
int real, imag;
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
OUTPUT: