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

Opps

oops project

Uploaded by

malakmaham4
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)
6 views

Opps

oops project

Uploaded by

malakmaham4
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/ 33

LAB 1

Program01:
Write a program to pass two variables as arguments to the user defined function to
calculate
their sum and print the result on the screen.
# include <iostream>
using namespace std;
void sum (int, int);
int main ()
{
int a, b;
cout<<"enter your value ? ";
cin >>a;
cout<<"enter your value ? ";
cin >>b;
sum (a, b);
cout<<"ok";
return 0;
}
void sum (int x, int y)
{
int s;
s = x + y;
cout<<"sum = "<<s<<endl;
}
OUTPUT:
Program02:
Write a program to convert the kilograms into grams by using a user-defined
function, return the result into calling function and print on the screen.
# include <iostream>
using namespace std;
int grams (int);
int main ()
{
int kg, gm;
cout<<"enter value in kilograms :OU";
cin>>kg;
gm = grams (kg);
cout<<"answer in grams = "<<gm;
return 0;
}
int grams (int val)
{
return val * 1000;
}
OUTPUT:
LAB 2
Program01:
Write a program having three functions i.e. “sum” and having different parameters (int,
double). Pass values to them and print the result on console using concept of function
overloading.

# include <iostream>
using namespace std;
int sum (int, int);
int sum (int, int, int);
float sum (double, double, double);

int main ()
{
cout<<"sum of 2 integer values = "<<sum (2, 4)<<endl;
cout<<"sum of 3 integer values = "<<sum (2, 4, 5)<<endl;
cout<<"sum of 3 float values = "<<sum (2.6, 4.7, 5.6)<<endl;
cout<<"ok";
return 0;
}
int sum (int x, int y)
{
return x + y;
}
int sum (int x, int y, int z)
{
return x + y + z;
}
float sum (double x, double y, double z)
{
return x + y + z;
}
OUTPUT:

Program02:
Write a program having two functions i.e. “mul” and having two and four
parameters integer type respectively. Pass values to them and calculate the
multiplication and print the result on console using concept of function overloading.
# include <iostream>
using namespace std;
int mul (int, int);
int mul (int, int, int, int);

int main ()
{
cout<<"sum of 2 integer values = "<<mul (4, 5)<<endl;
cout<<"sum of 4 integer values = "<<mul (1, 2, 3, 4)<<endl;
cout<<"ok";
return 0;
}
int mul (int x, int y)
{
return x * y;
}

int mul (int x, int y, int z, int i)


{
return x * y * z * i;
}
OUTPUT:

LAB 3
Progrm01:
Write a program to take two values from the user in main function. Pass those
values to the member function of the class and display the sum of those values by
using another member function of the class.
# include <iostream>
using namespace std;
class sum
{
private:
int n, m;
public:
void get (int a, int b)
{
n = a;
m = b;
}
void display (void)
{
cout<<"sum = "<<n+m;
}
};
int main ()
{
sum s;
int x, y;
cout<<"Enter first no. "; cin>> x;
cout<<"Enter second no. "; cin>> y;
s.get (x, y);
s.display ();
return 0;
}
OUTPUT:

Program02:
Write a program to define a class to input the name of a student and marks of three subjects
(s1, s2, s3), calculate the total marks and average marks in a member function of a class and
also display the output in another member function of a class. Each subject has a maximum of
100 marks.
# include <iostream>
using namespace std;
class st
{
private:
char name [15];
float s1, s2, s3, total, avg;
public:
void getrec (void)
{
cout <<"Enter name of student: "; cin>>name;
cout <<"Enter marks of 1st subject: ";cin>>s1;
cout <<"Enter marks of 2nd subject: ";cin>>s2;
cout <<"Enter marks of 3rd subject: ";cin>>s3;
total = s1+s2+s3;
avg = total/3.0;
}
void show (void)
{
cout <<"Enter name of student : "<<name<<endl;
cout <<"Enter marks of 1st subject : "<<s1<<endl;
cout <<"Enter marks of 2nd subject : "<<s2<<endl;
cout <<"Enter marks of 3rd subject : "<<s3<<endl<<endl;
cout <<"Total marks : "<<total<<endl;
cout <<"Avg marks : "<<avg<<endl;
}
};
int main ()
{
st abc;
abc.getrec ();
abc.show ();
return 0;
}
OUTPUT:

LAB 4
Program01:
Write a program to create three objects which uses a constructor (Default Constructor).

# include <iostream>
using namespace std;
class test
{
public:
test ()
{
cout<<"welcome"<<endl;
}
abc ()
{
cout<<"ok"<<endl;
}
};
int main ()
{
test a, b, c;
return 0;
}
OUTPUT:

Program02:
Write a program which uses a constructor (Copy Constructor).
#include<iostream>
using namespace std;

class test
{
private:
int a;
public:
test ()
{
a = 10;
}
test (test &z)
{
a=z.a;
}
void output()
{
cout<<" Value in variable a = "<<a<<endl;
}
};
int main()
{
test obj;
test obj1 (obj);
test obj2 (obj1);
obj.output();
obj1.output();
obj2.output();
return 0;
}
OUTPUT:
LAB 5
Program01:
Write a program to define two constructors, one will take three parameters and
second will take two parameters and then calculate the sum and also print the output
on the screen.
#include<iostream>
using namespace std;
class sum
{
public:
sum (int x, int y, int z)
{
}
cout<<"Sum of 3 integers is = "<<(x+y+z)<<endl;
sum (int x, int y)
{
cout<<"Sum of 2 integers is = "<<(x+y)<<endl<<endl;
}
};
int main ()
{
sum obj1 (6, 2), obj2 (7, 6, 8);
return 0;
OUTPUT:

Program 02: Write a program which uses constructor and destructor.


#include<iostream>
using namespace std;
class test
{
public:
test ()
{
cout<<”This is constructor function”<<endl;
}

~test ()
{
cout<<”This is destructor function”<<endl<<endl;
}
};

int main()
{
test obj;
int a, b;
a = 100;
b = 200;
cout<<”sum of two numbers is =” <<(a + b)<<endl;
return 0;
}
OUTPUT:

LAB 6
Program01:
Write a program which uses the concept of protected Inheritance.
#include<iostream>
using namespace std;
class A
{
private:
int a1, a2;
protected:
int pa1, pa2;
public:
void display(){
cout<<"Value of pa1 of Class A = "<<pa1<<endl;
cout<<"Value of pa2 of Class A = "<<pa2<<endl;}
};
class B: protected A
{
public:
void input()
{
cout<<"Enter the value of pa1: "<<endl;
cin>>pa1;
cout<<"Enter the value of pa2: "<<endl;
cin>>pa2;
cout<<endl<<"Value of pa1 of Class A = "<<pa1<<endl;
cout<<"Value of pa2 of Class A = "<<pa2<<endl;
}
};
int main(){
B obj;
obj.input();
return 0;
}
OUTPUT:

Program02:
Write a program which uses the concept of Inheritance.
#include<iostream>
using namespace std;
class student
{
private:
char name[20], address[30];
public:
void input()
{
cout<<"Enter name: "<<endl;
cin>> name;
cout<<"Enter address: "<<endl;
cin>> address;
}
void display()
{

cout<<endl<<"Name: "<<name<<endl;
cout<<"Address: "<<address<<endl;
}
};
class marks: public student
{
private:
int oop, ds, eng, tot;
public:
void inputmarks()
{
cout<<"Enter obtained marks in oop: "<<endl;
cin>>oop;
cout<<"Enter obtained marks in discrete structure: "<<endl;
cin>>ds;
cout<<"Enter obtained marks in English: "<<endl; cin>>eng;
tot = oop + ds + eng;}
void displaymarks()
{
cout<<"Obtained marks in oop: "<<oop<<endl;
cout<<"Obtained marks in discrete structure: "<<ds<<endl;
cout<<"Obtained marks in English: "<<eng<<endl;
cout<<endl<<"Total obtained marks: "<<tot;
}
};
int main()
{
marks obj;
obj.input();
obj.inputmarks();
obj.display();
obj.displaymarks();
return 0;
}
OUTPUT:
LAB 7
Program01:
Late binding:
# include <iostream>
using namespace std;
class bb
{
public:
virtual void ppp ()=0;
};
class d1 : public bb
{
public:
void ppp ()
{
cout<< "It is the first derived class"<<endl;
}
};
class d2 : public bb
{
public:
void ppp ()
{
cout<< "It is the second derived class"<<endl;
}
};
int main ()
{
bb *p;
d1 a;
d2 b;
p = &a;
p -> ppp ();
p = &b;
p -> ppp ();
return 0;
}
OUTPUT:
Program02:
Early binding:
# include <iostream>
using namespace std;
class bb
{
public:
void ppp ()
{
cout<< "Hello, it is the base class"<<endl;
}
};
class d1 : public bb
{
public:
void ppp ()
{
cout<< "It is the first derived class"<<endl;
}
};
class d2 : public bb
{
public:
void ppp ()
{
cout<< "It is the second derived class"<<endl;
}
};
int main ()
{
bb *p;
d1 a;
d2 b;
p = &a;
p -> ppp ();
p = &b;
p -> ppp ();
return 0;
}
OUTPUT:

LAB 8
Single Inheritance:

#include <iostream>
using namespace std;
class base //single base class
{
public:
int x;
void getdata()
{
cout << "Enter the value of x = "; cin >> x;
}
};
class derive : public base //single derived class
{
private:
int y;
public:
void readdata()
{
cout << "Enter the value of y = "; cin >> y;
}
void product()
{
cout << "Product = " << x * y;
}
};
int main()
{
derive a; //object of derived class
a.getdata();
a.readdata();
a.product();
return 0;
}
OUTPUT:

Multiple Inheritance:
#include <iostream>
using namespace std;
class A
{
public:
int x;
void getx()
{
cout << "enter value of x: "; cin >> x;
}
};
class B
{
public:
int y;
void gety()
{
cout << "enter value of y: "; cin >> y;
}
};
class C : public A, public B //C is derived from class A and class B
{
public:
void sum()
{
cout << "Sum = " << x + y;
}
};
int main()
{
C obj1; //object of derived class C
obj1.getx();
obj1.gety();
obj1.sum();
return 0;
}
OUTPUT:

LAB 9
Program 01:
Write a program to increment the value of object without the concept of operator overloading.
#include<iostream>
using namespace std;
class counter
{
private:
int count;
public:
counter ()
{
count=0;
}
void incount ()
{
++count;
}
int getcount ()
{
return count;
}
};
int main ()
{
counter c1, c2;
cout<<"c1 = "<<c1.getcount()<<endl;
cout<<"c2 = "<<c2.getcount()<<endl;
c1.incount();
c2.incount();
c2.incount();
c2.incount();
cout<<"c1 = "<<c1.getcount()<<endl;
cout<<"c2 = "<<c2.getcount();
return 0;
}
OUTPUT:
Program02:
Write a program to decrement the value of object without the concept of operator overloading.
#include<iostream>
using namespace std;
class counter{
private:
int count;
public:
counter ()
{
count=0;
}
void incount ()
{ --count;
}
int getcount ()
{
return count;
}
};
int main ()
{
counter c1, c2;
cout<<"c1 = "<<c1.getcount()<<endl;
cout<<"c2 = "<<c2.getcount()<<endl;
c1.incount();
c2.incount();
c2.incount();
c2.incount();
c2.incount();
cout<<"c1 = "<<c1.getcount()<<endl;
cout<<"c2 = "<<c2.getcount();
return 0;
}
OUTPUT:

LAB 10
Program 01:
Write a program to add the distance of two objects without the concept of operator
overloading.

#include<iostream>
using namespace std;
class dist
{
private:
int feet, inches;
public:
dist()
{
}
dist(int f, int i)
{
feet = f;
inches = i;
}
void getdist()
{
cout<<"Enter feet: ";
cin>>feet;
cout<<"Enter inches: ";
cin>>inches;
}
void adddist(dist d1, dist d2)
{
feet = d1.feet + d2.feet;
inches = d1.inches + d2.inches;
}
void showdist()
{
cout<<feet<<"'"<<inches;
}
};
int main()
{
dist dist1, dist2(6,5), dist3;
dist1.getdist();
dist3.adddist(dist1, dist2);
cout<<"dist 1 = ";
dist1.showdist();
cout<<endl<<"dist 2 = ";
dist2.showdist();
cout<<endl<<"dist 3 = ";
dist3.showdist();
return 0;
}
OUTPUT:

Program 02:
Write a program to add the distance of two objects by using the concept of operator overloading i-e to
overload + operator.
#include<iostream>
using namespace std;
class dist
{
private:
int feet, inches;
public:
dist()
{
}
dist(int f,int i)
{
feet = f;
inches = i;
}
void getdist()
{
cout<<" enter feet ";
cin>>feet;
cout<<" enter inches ";
cin>>inches;
}
dist operator+(dist d2)
{
int f, in;
f = feet + d2.feet;
in = inches + d2.inches;
return dist(f,in);
}
void showdist()
{
cout<<feet<<" \" "<<inches<<" \' ";
}
};
int main(){
dist dist1, dist2(6,5), dist3;
dist1.getdist();
dist3=dist1+dist2;
cout<<endl<<" dist 1 = ";
dist1.showdist();
cout<<endl<<" dist 2 = ";
dist2.showdist();
cout<<endl<<" dist 3 = ";
dist3.showdist();
return 0;
}
OUTPUT:

LAB 11
Program01:
Encapsulation:
#include <iostream>
using namespace std;
class Employee
{
private:
// Private attribute
int salary;
public:
// Setter
void setSalary (int s)
{
salary = s;
}
// Getter
int getSalary ()
{
return salary;
}
};
int main ()
{
Employee myObj;
myObj.setSalary (50000);
cout << myObj.getSalary ();
return 0;
}
OUTPUT:

Program02:
Encapsulation:
#include <iostream>
using namespace std;
class Employee
{
private:
int empId;
string empName;
float empSalary;
public:
void setEmpId(int id)
{
empId = id;
}
void setEmpName(string name)
{
empName = name;
}
void setEmpSalary(float salary)
{
empSalary = salary;
}
int getEmpId()
{
return empId;
}
string getEmpName()
{
return empName;
}
float getEmpSalary()
{
return empSalary;
}
};
int main ()
{
Employee emp;
emp.setEmpId(101);
emp.setEmpName("John Doe");
emp.setEmpSalary(5000.0);
cout << "Employee ID: " << emp.getEmpId() << endl;
cout << "Employee Name: " << emp.getEmpName() << endl;
cout << "Employee Salary: " << emp.getEmpSalary() << endl;
return 0;
}
OUTPUT:

THE END

You might also like