0% found this document useful (0 votes)
11 views19 pages

C++ Function Overloading and Classes

Uploaded by

murudkarp11
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views19 pages

C++ Function Overloading and Classes

Uploaded by

murudkarp11
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 "<<[Link]();
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;
[Link](8);
[Link](6);

cout<<"Length of rectangle "<<[Link]()<<endl;


cout<<"Breadth of rectangle "<<[Link]()<<endl;

cout<<"Area of rectangle "<<[Link]()<<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 = [Link] + [Link];
imag = [Link] + [Link];
}
};

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;
[Link]();
[Link]();
[Link]();
[Link]();
[Link](c1,c2);
cout<<endl;
cout<<"Addition of two complex number is "<<endl;
[Link]();
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 "<<[Link]<<endl;
}

int main(){
Equitri et;
[Link](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 "<<[Link]<<endl;
}
int main(){
Equitri et;
[Link](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;
[Link]();
[Link]();
Student s2;
[Link]();
[Link]();
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;
[Link]();

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 = ";

[Link]();

cout << "Point p2 = ";

[Link]();

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 = [Link];

// 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 : ";

[Link]();

cout << "\n id of B : ";


[Link]();

cout << "\n id of C : ";

[Link]();

cout << "\n id of D : ";

[Link]();

return 0;

You might also like