Exp 1
//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 ref and return by ref
#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 : -
EXP 02: -
//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: -
EXP 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: -
//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: -
EXP 04
//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 : -
//DESTRUCTOR
#include <iostream> // Corrected header file inclusion
using namespace std;
int count = 0; // Global variable to keep track of object count
class test {
public:
// Constructor
test() {
count++;
cout << "\n\nConstructor Msg: Object number " << count << " created..";
// Destructor
~test() {
cout << "\n\nDestructor Msg: Object number " << count << " destroyed..";
count--;
};
int main() {
cout << "Inside the main block..";
cout << "\n\nCreating first object T1..";
test T1;
// BLOCK.1
cout << "\n\nInside BLOCK 1..";
cout << "\n\nCreating two more objects T2 and T3..";
test T2, T3;
cout << "\n\nLeaving Block 1..";
} // T2 and T3 are destroyed here
cout << "\n\nBack inside the main block.."; return 0; }
//PARAMETERIZED CONSTRUCTOR
#include <iostream> // Corrected header file inclusion
using namespace std;
class Point {
int x, y; // Private members
public:
// Inline parameterized constructor definition
Point(int a, int b) {
x = a;
y = b;
// Function to display the point
void display() {
cout << "(" << x << "," << y << ")\n";
};
int main() {
Point p1(1, 1); // Invokes parameterized constructor
Point p2(5, 10);
cout << "Point p1 = ";
p1.display();
cout << "Point p2 = ";
p2.display();
return 0;
}
//COPY CONSTRUCTOR
#include <iostream> // Corrected header file inclusion
using namespace std;
class code {
int id; // Private member variable
public:
// Default constructor
code() { }
// Parameterized constructor
code(int a) {
id = a;
// Copy constructor
code(const code &x) {
id = x.id;
// Function to display the id
void display() {
cout << id;
};
int main() {
code A(100); // Invokes parameterized constructor
code B(A); // Invokes copy constructor
code C = A; // Invokes copy constructor
code D; // Invokes default constructor
D = A; // Invokes default assignment operator
cout << "\n id of A : ";
A.display();
cout << "\n id of B : ";
B.display();
cout << "\n id of C : ";
C.display();
cout << "\n id of D : ";
D.display();
return 0;