Professional Documents
Culture Documents
Pranav Pankaj 18107024 Oops - Lab.record
Pranav Pankaj 18107024 Oops - Lab.record
Pranav Pankaj 18107024 Oops - Lab.record
PRANAV PANKAJ
SUBMITTED TO –
Dr. RAJESH MAHULE
DEPARTMENT OF INFORMATION TECHNOLOGY,
#include<iostream>
using namespace std;
int main()
{
int mark[5], i;
float sum=0;
cout<<"\nEnter marks obtained in Physics, Chemistry, Maths, CS, English :: \n";
for(i=0; i<5; i++)
{
cout<<"\nEnter mark[ "<<i+1<<" ] :: ";
cin>>mark[i];
sum=sum+mark[i];
}
float avg=sum/5;
float perc;
perc=(sum/500)*100;
cout<<"\nAverage Marks of 5 Subjects = [ "<<avg<<" ] \n";
cout<<"\nPercentage in 5 Subjects = [ "<<perc<<"% ] \n";
return 0;
OUTPUT:
Enter marks obtained in Physics, Chemistry, Maths, CS, English ::
Enter mark[ 1 ] :: 79
Enter mark[ 2 ] :: 87
Enter mark[ 3 ] :: 67
Enter mark[ 4 ] :: 99
Enter mark[ 5 ] :: 80
#include <iostream>
using namespace std;
int main( ) {
char ary[] = "Welcome to C++ tutorial";
cout << "Value of ary is: " << ary << endl;
}
Output:
int main () {
float p, r, t, ci(1);
cout<<"Enter principal amount, rate of interest and years to evaluate CI: ";
cin>>p>>r>>t;
ci *= (100 + r)/100;
ci *= p;
ci -= p;
return 0;
}
OUTPUT:
Enter principal amount, rate of interest and years to evaluate CI: 200 40 2
int main()
{
unsigned int n;
unsigned long long factorial = 1;
cout << "Factorial of " << n << " = " << factorial;
return 0;
}
OUTPUT:
Enter a positive integer: 12
Factorial of 12 = 479001600
int main()
{
int n, t1 = 0, t2 = 1, nextTerm = 0;
OUTPUT:
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
#include "iostream"
int main () {
int a, b;
long pow(1);
cout<<a<<"^"<<b<<" :"<<pow;
return 0;
}
OUTPUT:
Enter the values (a, b): 10 5
10^5 :100000
int main () {
int n;
int main()
{
int a = 5, b = 10, temp;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;
return 0;
}
OUTPUT:
Before swapping.
a = 5, b = 10
After swapping.
a = 10, b = 5
9.WAP using inline function.
#include <iostream>
using namespace std;
inline int cube(int s)
{
return s*s*s;
}
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;
OUTPUT:
The cube of 3 is: 27
int& returnValue(int& x) {
return x;
}
int main() {
int a = 20;
int &b = returnValue(a);
cout<<"a = "<<a<<" The address of a is "<<&a<< endl;
returnValue(a) = 13;
class Test {
int a, b;
public:
Test() {}
void Test::getter () {
cout<<"a: "<<a<<" b: "<<b;
}
int main () {
Test t;
t.setter(3, 5);
t.getter();
return 0;
}
OUTPUT:
a: 3 b: 5
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
class DM
{
public:
double meter,centimeter;
};
class DB
{
public:
double feet,inches;
friend void add(DM,DB);
};
class Time
{
private:
int hours;
int minutes;
int seconds;
public:
void getTime(void);
void putTime(void);
void addTime(Time T1,Time T2);
};
void Time::getTime(void)
{
cout << "Enter time:" << endl;
cout << "Hours? "; cin>>hours;
cout << "Minutes? "; cin>>minutes;
cout << "Seconds? "; cin>>seconds;
}
void Time::putTime(void)
{
cout << endl;
cout << "Time after add: ";
cout << hours << ":" << minutes << ":" << seconds << endl;
}
this->seconds=T1.seconds+T2.seconds;
this->minutes=T1.minutes+T2.minutes + this->seconds/60;;
this->hours= T1.hours+T2.hours + (this->minutes/60);
this->minutes %=60;
this->seconds %=60;
}
int main()
{
Time T1,T2,T3;
T1.getTime();
T2.getTime();
//add two times
T3.addTime(T1,T2);
T3.putTime();
return 0;
}
OUTPUT:
Enter time:
Hours? 22
Minutes? 44
Seconds? 55
Enter time:
Hours? 12
Minutes? 50
Seconds? 45
Time after add: 35:35:40
14.Create a class Student with a string variable 'name' and an integer
variable 'roll no.'. Assign the value of roll no as '2' and name as 'John' by
creating an object of the class;
#include "iostream"
class Student {
int rno;
string name;
public:
Student(string _name, int _rno): name(_name), rno(_rno) {}
void print() {
cout<<"Student\nRoll No. : "<<this->rno<<"\nName: "<<this->name<<endl<<endl;
}
~Student() {}
};
int main () {
Student s("John", 2);
s.print();
return 0;
}
OUTPUT:
Student
Roll No. : 2
Name: John
15.Write a program to print area of rectangle by creating a class named
Area which have two functions as setDim which takes length and breadth
of the rect as parameters and getArea which returns the area of the
rectangle.
#include "iostream"
//global id;
int id(1);
class Area {
float l, b;
public:
Area() {}
float getArea () {
return l*b;
}
~Area() {}
};
int main () {
Area r;
float l, b;
Area of rectangle : 30
16.Write a program that would print the info (name, year of joining, salary,
address) of 3 employees by creating a class named Employee as the table.
#include "iostream"
//global id;
int id(1);
class Emp {
string name, yoj, address;
public:
Emp() {}
~Emp() {}
};
int main () {
Emp e[3];
class A {
int a, b;
public:
A(): a(0), b(0) {
cout<<"Object created.\n";
}
A(int a1, int b1): a(a1), b(b1) {
cout<<"Object created using parameterised constructor.\n";
}
A(A &temp): a(temp.a), b(temp.b) {
cout<<"Object created using copy constructor.\n";
}
void display() {
cout<<"Value of a: "<<a<<endl;
cout<<"Value of b: "<<b<<endl;
}
~A() {
cout<<"Object destroyed.\n";
}
};
int main () {
// intialising using parameterised constructor
A g(1, 2);
class Mark {
int marks;
public:
Mark (): marks(0) {}
Mark (int _marks): marks(_marks) {}
Mark (Mark &m): marks(m.marks) {}
int main () {
Mark s1(80), s2(34), s3(79);
cout<<s1.greatest(s2, s3);
return 0;
}
OUTPUT:
MARKS:80
19.Create two objects of a class with two integer type members. Compare
the two operators have same values by overloading "==" operator.
#include "iostream"
class Coor {
int x, y;
public:
Coor(): x(0), y(0) {}
Coor(int _x, int _y): x(_x), y(_y) {}
Coor(Coor &c): x(c.x), y(c.y) {}
friend ostream & operator << (ostream &out, Coor const &c) {
out<<"( "<<c.x<<", "<<c.y<<" )";
return out;
}
~Coor() {}
};
int main () {
Coor p1, p2;
p1 = Coor(a1, b1);
p2 = Coor(a2, b2);
if (p1 == p2)
cout<<"Coordinates are equal.";
else
cout<<"Coordinates are not equal.";
return 0;
}
OUTPUT:
20.Create a class Float that contains one float data member. Overload all
the four arithmetic operators so that they can operate on object(s) of Float
(class).
#include "iostream"
class Float {
float f;
public:
Float(): f(0) {}
Float(float _f): f(_f) {}
Float(Float &F): f(F.f) {}
friend ostream & operator << (ostream &out, Float const &F) {
out<<F.f;
return out;
}
~Float() {}
};
int main () {
Float d(5), e(4.3);
cout<<"After overloading \'*\' operator - (d/e) : ";
cout<<d*e<<endl;
cout<<"After overloading \'-\' operator - (d/e) : ";
cout<<d-e<<endl;
cout<<"After overloading \'+\' operator - (d/e) : ";
cout<<d+e<<endl;
cout<<"After overloading \'/\' operator - (d/e) : ";
cout<<d/e<<endl;
return 0;
}
OUTPUT:
21.Write a program to overload << and >> operators for oject of class Time.
- Write a program overload <=, ==, >= operators to compare the objects of
class Time
#include "iostream"
class Time {
int hh, mm, ss;
public:
Time(): hh(0), mm(0), ss(0) {}
Time(int _hh, int _mm, int _ss): hh(_hh), mm(_mm), ss(_ss) {}
Time(Time &t): hh(t.hh), mm(t.mm), ss(t.ss) {}
friend ostream & operator << (ostream &out, Time &t) {
out<<t.hh<<"h "<<t.mm<<"m "<<t.ss<<"s";
return out;
}
int main () {
Time T1, T2;
cout<<"+++++++++++++++++++++++++++++++++++++++\n";
cout<<"overloaded function call operator or (): \n";
T1(4);
cout<<T1;
return 0;
}
OUTPUT:
//global variable
const int maxSIZE = 50;
class safearay {
int arr[maxSIZE];
int size;
public:
safearay(int _i): size(_i) {
register int i;
return arr[i];
}
};
int main() {
safearay A(10);
return 0;
}
OUTPUT:
Value of A[2] : 2
Value of A[5] : 5
Value of A[12] : Index out of bounds
0
23.Convert Class Time type data to integer type and vice-versa.
#include "iostream"
class Time {
int hh, mm, ss;
public:
Time(): hh(0), mm(0), ss(0) {}
Time(int _hh, int _mm, int _ss): hh(_hh), mm(_mm), ss(_ss) {}
Time(Time &t): hh(t.hh), mm(t.mm), ss(t.ss) {}
friend ostream & operator << (ostream &out, Time &t) {
out<<t.hh<<"h "<<t.mm<<"m "<<t.ss<<"s";
return out;
}
operator int () {
return int(this->hh);
}
~Time() {}
};
int main () {
Time T1, T2;
int x = int(T1);
T1(5);
cout<<"\nInt to object type: "<<T1;
return 0;
}
OUTPUT:
Feed time T1: 5
26
9
Time T1 : 5h 26m 9s
class Student {
int rollno;
string name;
double curr;
public:
Student(): curr(0) {}
Student(string _name): name(_name), rollno(Groll), curr(0) { Groll++; }
void pay();
void result();
~Student() {}
};
public:
Fees(): fees(0) {}
// Fees(double _fees): fees(_fees) {}
void reciept(string _name, int num) {
cout<<"+++++++++++++++++Reciept++++++++++++++++\n\n";
cout<<"Name: "<<_name<<endl<<"Roll Number: "<<num<<"\n\nFee paid successfully. Amount:
"<<fees<<endl<<endl;
}
double payTrigger() {
fees += 10000;
return fees;
}
~Fees() {}
};
public:
Result(): per(80) {}
int score() {
return this->per;
}
~Result() {}
};
void Student::pay() {
Fees f;
this->curr += f.payTrigger();
f.reciept(this->name, this->rollno);
}
void Student::result() {
Result r;
cout<<"You got "<<r.score()<<"% \n";
}
int main () {
Student s("Aditya");
s.pay();
s.result();
return 0;
}
OUTPUT:
+++++++++++++++++Reciept++++++++++++++++
Name: Pranav
Roll Number: 24
Fee paid successfully. Amount: 10000
++++++++
+ Staff +
++++++++
+ code, Name +
++++++++
/ | \
/ | \
/ | \
\/ \|/ \/
++++++++ ++++++++ ++++++++
+ Teacher + + Typist + + Officer +
++++++++ ++++++++ ++++++++
+ Subject, Pub+ + Speed + + Grade +
++++++++ ++++++++ ++++++++
/ \
/ \
\/ \/
++++++++ ++++++++
+ Regular + + Casual +
++++++++ ++++++++
+ + + Daily wages +
++++++++ ++++++++
#include "iostream"
int Gcode(0);
class Staff {
int code;
string name;
public:
Staff(): name("No_name"), code(0) {code++;}
Staff(string _name) {code++;}
~Staff() {}
};
public:
Teacher() {
cout<<"Teacher registered\n";
}
~Teacher() {}
};
public:
Typist() {
}
void wpm() {
cout<<"Your speed is: "<<this->speed<<" wpm\n";
}
~Typist() {}
};
public:
Officer(): grade('A') {
cout<<"Officer of grade: A registered\n";
}
Officer(char _grade): grade(_grade) {
cout<<"Officer of grade: "<<this->grade<<" registered\n";
}
~Officer() {}
};
class COMPLEX {
int real, img;
public:
COMPLEX(): real(0), img(0) {}
COMPLEX( COMPLEX &c ): real(c.real), img(c.img) {}
COMPLEX(int _real, int _img): real(_real), img(_img) {}
void show () {
char c = '+';
if (img < 0)
c = '-';
int main () {
COMPLEX c1(2, 4), c2(4, 5);
cout<<"C1 : ";
c1.show();
cout<<"C2 : ";
c2.show();
//global id;
int id(1);
class Student {
int rno;
string name;
public:
Student(): name("Unknown"), rno(id) {id++;}
Student(string _name): name(_name), rno(id) {id++;}
Student(Student &s): name(s.name), rno(s.rno + 1) {id++;}
void print() {
cout<<"Student\nRoll No. : "<<this->rno<<"\nName: "<<this->name<<endl<<endl;
}
~Student() {}
};
int main () {
Student s1("Rohan"), s2;
s1.print();
s2.print();
return 0;
}
OUTPUT:
Student
Roll No. : 1
Name: Rohan
Student
Roll No. : 2
Name: Unknown