Oops1 4

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 15

Experiment No.

//FUNCTION OVERLOADING

#include<iostream>
using namespace std;
void number(int a){
cout<<a<<endl;
}

void number(float a){


cout<<a<<endl;
}
int main(){
int a =9;
float b =7.8;
number(a);

number(b);

return 0;
}

Output : -
//Inline function and Default Argument

#include<iostream> using namespace std ;

bool cmp(int a ,int b){


return a>b?a:b;
}
void print(int a , int b =9,int c =8){ for(int i =0;i<a;i++){

cout<<"*"<<" ";
}
for(int i=0;i<b;i++){
cout<<"&"<<" ";
}
for(int i =0 ;i<c;i++){
cout<<"#"<<" ";
}

}
int main(){
bool ans = cmp(5,4);
cout<<ans<<endl;
print(4);
cout<<endl;
print(4,5);
cout<<endl;
print(4,5,6);
return 0;
}

Output :
//CALL BY REFERENCE AND RETURN BY REFERENCE

#include<iostream>
using namespace std;
void inc(int &a){

a ++;
cout<<"The increased value of a "<<a<<endl;

int main(){
int x =9;
inc(x);
cout<<endl;

cout<<"Main function value of x "<<x<<endl; return 0 ;


}

Output :
Experiment No. 2

//Encapsulation
//Structure limitation and how class overcome it

#include<iostream>
using namespace std ;

class Rectangle{
public:
int length;
int breadth;

Rectangle(int len , int bre){


length =len;
breadth =bre;
}
int getArea(){
return length*breadth;
}
};

int main(){
Rectangle rec(5,4);
cout<<"Area of rectangle "<<rec.getArea();
return 0;
}

Output: -
//Data hiding
#include<iostream>
using namespace std;

class Rectangle{

private:
int lenght;
int breadth;

public:
void setLen(int len){
lenght = len;
}

void setBre(int bre){


breadth =bre;
}

int getLen(){
return lenght;
}

int getbre(){
return breadth;
}

int getArea(){
return lenght * breadth;
}

};

int main(){
Rectangle rect;
rect.setLen(8);
rect.setBre(6);

cout<<"Length of rectangle "<<rect.getLen()<<endl;


cout<<"Breadth of rectangle
"<<rect.getbre()<<endl;

cout<<"Area of rectangle
"<<rect.getArea()<<endl; return 0;
}

OUTPUT:
/ Array of object
#include <iostream>
using namespace std;
class Employee
{

int id;
int salary;

public:
void setid()
{
salary = 876;
cout << "Enter the id " <<
endl; cin >> id;
}

void getid()
{
cout << "Employee id is " << id << endl;
}
};
int main()
{
Employee fb[4];

for (int i = 0; i < 4; i++)


{
fb[i].setid();
fb[i].getid();
}
return 0;
}

Output:
Experiment no.3

// Pass an object as an argument

#include <iostream>
using namespace std;
class ComplexNo
{
int real;
int imag;

public:
void getdata();
void setdata();
void add(ComplexNo c1, ComplexNo c2)
{
real = c1.real + c2.real;
imag = c1.imag + c2.imag;
}
};

void ComplexNo ::setdata()


{ cout<<"Enter the Real
PART"<<endl; cin>>real;
cout<<"Enter the Imag
Part"<<endl; cin>>imag;
}
void ComplexNo :: getdata()
{ cout<<real<<"+"<<imag<<"i"<<
endl; }
int main()
{
ComplexNo c1;
ComplexNo c2;
ComplexNo c3;
c1.setdata();
c1.getdata();
c2.setdata();
c2.getdata();
c3.add(c1,c2);
cout<<endl;
cout<<"Addition of two complex number is "<<endl;
c3.getdata();
return 0;
}

Output:
//Friend function

#include<iostream>
using namespace std;

class Equitri{
float a;
float perimeter;

public:
void setA(float len){
a =len;
perimeter = a*3;
}
friend void
printPerimeter(Equitri); };

void printPerimeter(Equitri et)


{ cout<<"Perimeter is
"<<et.perimeter<<endl;
}

int main(){
Equitri et;
et.setA(3);
printPerimeter(et);
return 0;
}

Output:
Experiment No.4

//Static data members


#include <iostream>
#include <string>
using namespace std;

class Student
{
private:
string name;
int rollno;

public:
static int objectCount;

void setter()
{
cout << "Enter the name of student " << endl;
cin >> name;
cout << "Enter the rollno of student " << endl;
cin >> rollno;
objectCount++;
}

void getter()
{
cout << "Name " << name <<
endl; cout << "Rollno " << rollno
<< endl;
}
};
int Student::objectCount;

int main()
{
Student s1;
s1.setter();
s1.getter();
Student s2;
s2.setter();
s2.getter();
cout << "Objects " << Student::objectCount << endl;

return 0;
}

Output:
// Static data functions

#include <iostream>
using namespace std;
class Note
{
static int num;

public:
static void func()
{
cout << "The value of num is " << num << endl;
}
};
int Note::num = 90;
int main()
{
Note n;
n.func();

return 0;
}

Output :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy