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

OOP Lab3

The document contains examples demonstrating encapsulation and function overloading in C++. The encapsulation example defines a stack class with private data members and public member functions to initialize, push, and pop elements from the stack. The function overloading examples show defining multiple functions with the same name but different parameters to handle integers, doubles, and longs for an absolute value function, and to calculate the areas of circles, triangles, and rectangles.

Uploaded by

Tushaar Sengupta
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

OOP Lab3

The document contains examples demonstrating encapsulation and function overloading in C++. The encapsulation example defines a stack class with private data members and public member functions to initialize, push, and pop elements from the stack. The function overloading examples show defining multiple functions with the same name but different parameters to handle integers, doubles, and longs for an absolute value function, and to calculate the areas of circles, triangles, and rectangles.

Uploaded by

Tushaar Sengupta
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

/// Program 1: Encapsulation demo

#include <iostream>
using namespace std;

#define SIZE 100

// This creates the class stack.


class stack {
int stck[SIZE];
int tos;
public:
void init();
void push(int i);
int pop();
};

void stack::init()
{
tos = 0;
}

void stack::push(int i)
{
if(tos==SIZE)
{
cout << "Stack is full.\n";
return;
}
stck[tos] = i;
tos++;
}

int stack::pop()
{
if(tos==0) {
cout << "Stack underflow.\n";
return 0;
}
tos--;
return stck[tos];
}

int main()
{
stack stack1, stack2; // create two stack objects

stack1.init();
stack2.init();

stack1.push(10);
stack2.push(20);

stack1.push(30);
stack2.push(40);

cout << stack1.pop() << " ";


cout << stack1.pop() << " ";
cout << stack2.pop() << " ";
cout << stack2.pop() << "\n";

return 0;
}

/////////////////// Function Overloading

#include <iostream>
using namespace std;

// abs is overloaded three ways


int abs_vit(int i);
double abs_vit(double d);
long abs_vit(long l);

int main()
{
cout << abs_vit(-10) << "\n";

cout << abs_vit(-11.0) << "\n";

cout << abs_vit(-9L) << "\n";

return 0;
}

int abs_vit(int i)
{
cout << "Using integer abs()\n";

return i<0 ? -i : i;
}

double abs_vit(double d)
{
cout << "Using double abs()\n";

return d<0.0 ? -d : d;
}

long abs_vit(long l)
{
cout << "Using long abs()\n";

return l<0 ? -l : l;
}

///////////////////////

Task: Area of circle, triangle, rectangle using


function overloading
name: Area123

circle-> radius
triangle -> a,b,c
rectangle -> b,h

#include <iostream>
using namespace std;

float Area123(int i);


float Area123(int a, int b, int c);
float Area123(int b, int h);

int main()
{
cout << Area123(10) << "\n";

cout << Area123(10,10,10) << "\n";

cout << Area123(2,3) << "\n";

return 0;
}

float Area123(int i)
{
//cout << "Using integer abs()\n";

return 3.14*i*i;
}

float Area123(int a,int b,int c)


{
//cout << "Using double abs()\n";

return 12344;
}

float Area123(int b,int h)


{
//cout << "Using long abs()\n";

return 0.5*b*h;
}

/// References
1. Problem Solving with C++: The Object of Programming, second edition, Walter J.
Savitch, Addison Wesley Publishers, 1999.
2. Object-oriented programming with C++ by E.Balagurusamy, 2nd Edition, TMH.
3. Object Oriented Design by Rumbaugh (Pearson publication)
4. OOPS C++ Big C++ Cay Horstmann Wiley Publication
5. https://www.w3schools.com/
6. https://www.tutorialspoint.com/index.htm
7. https://www.javatpoint.com/
8. https://www.geeksforgeeks.org/
9. Bjarne Stroustrup, The Design and Evolution of C++, Addison-Wesley.

You might also like