0% found this document useful (0 votes)
19 views22 pages

Oops

The document contains C++ programming experiments focusing on various concepts such as function overloading, inline functions, call by reference, encapsulation, data hiding, and static members. It includes code examples demonstrating these concepts, including a calculator class, access specifiers, and an array of objects. Each section provides code snippets along with explanations of the output and functionality.
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)
19 views22 pages

Oops

The document contains C++ programming experiments focusing on various concepts such as function overloading, inline functions, call by reference, encapsulation, data hiding, and static members. It includes code examples demonstrating these concepts, including a calculator class, access specifiers, and an array of objects. Each section provides code snippets along with explanations of the output and functionality.
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/ 22

Experiment 1

Aim: a. Write program in C++ for Function Overloading.


( Polymorphism)
b. Write program in C++ for Inline Functions and Default argument
functions.

Code:

Inline function:

#include <iostream>

using namespace std;

inline int cube(int s)

return s*s*s;

int main()

cout << "The cube of 3 is: " << cube(3) << "\n";

return 0;

Output:
Default argument:

Output
c. Write program in C++ for call by reference and return by
reference.

call by reference:

#include <iostream>

using namespace std;

void swap(int& x, int& y)

int z = x;

x = y;

y = z;

int main()

int a = 45, b = 35;

cout << "Before Swap\n";

cout << "a = " << a << " b = " << b << "\n";

swap(a, b);

cout << "After Swap with pass by reference\n";

cout << "a = " << a << " b = " << b << "\n";

Output
Return by reference:

#include <iostream>

using namespace std;

// Global variable

int num=59;

// Function declaration

int& test();

int main()

test() = 5;

cout << num;

return 0;

int& test()

return num;

}
Experiment 2
a. Expose the limitations of structure and how it can overcome by
using class. Write a program in c++ where you use different
datatypes as data members of class. (Encapsulation

#include <iostream>

using namespace std;

class Calculator {

private:

float num1; // float used for decimal support

float num2;

char op; // operator: +, -, *, /

public:

void getInput() {

cout << "Enter first number: ";

cin >> num1;

cout << "Enter second number: ";

cin >> num2;

cout << "Enter operator (+, -, *, /): ";

cin >> op;

void performOperation() {

float result;

switch (op) {

case '+':

result = num1 + num2;

cout << "Result: " << result << endl;

break;
case '-':

result = num1 - num2;

cout << "Result: " << result << endl;

break;

case '*':

result = num1 * num2;

cout << "Result: " << result << endl;

break;

case '/':

if (num2 != 0) {

result = num1 / num2;

cout << "Result: " << result << endl;

} else {

cout << "Error: Division by zero!" << endl;

break;

default:

cout << "Invalid operator!" << endl;

};

int main() {

Calculator calc;

calc.getInput();

calc.performOperation();

return 0;

}
Output
b. WAP to use public, private, protected access specifiers. (Data
Hiding)

Public:

#include<iostream>

using namespace std;

// class definition

class Circle

public:

double radius;

double compute_area()

return 3.14*radius*radius;

};

// main function

int main()

Circle obj;

// accessing public data member outside class

obj.radius = 5.5;
cout << "Radius is:" << obj.radius << "\n";

cout << "Area is:" << obj.compute_area();

return 0;

}
Private:

#include<iostream>

using namespace std;

class Circle

// private data member

private:

double radius;

// public member function

public:

double compute_area()

{ // member function can access private

// data member radius

return 3.14*radius*radius;

};

// main function

int main()

// creating object of the class

Circle obj;

// trying to access private data member


// directly outside the class it will give error

obj.radius = 1.5;

cout << "Area is:" << obj.compute_area();

return 0;

Output:

Program:---same as above and no error

#include<iostream>

using namespace std;

class Circle

private:

double radius;

public:
double compute_area(double r)

radius = r;

double area = 3.14*radius*radius;

cout << "Radius is:" << radius << endl;

cout << "Area is: " << area;

return area;

};

// main function;

int main()

// creating object of the class

Circle obj;

// trying to access private data member

// directly outside the class

obj.compute_area(1.5);

return 0;

}
Protected:

// C++ program to demonstrate protected access modifier

#include <bits/stdc++.h>

using namespace std;

// base class

class Parent

// protected data members

protected:

int id_protected;

};

// sub class or derived class

class Child : public Parent

public:

void setId(int id)

// Child class is able to access the inherited

// protected data members of base class

id_protected = id;

void displayId()

cout << "id_protected is: " << id_protected << endl;


}

};

// main function

int main()

Child obj1;

// member function of derived class can access the

// protected data members of base class

obj1.setId(81);

obj1.displayId();

return 0;

}
c. WAP to demonstrate array of objects.

#include <iostream>

using namespace std;

class Student {

public:

int rollNo;

string name;

void getDetails() {

cout << "Enter Roll No: ";

cin >> rollNo;

cout << "Enter Name: ";

cin >> name;

void displayDetails() {

cout << "Roll No: " << rollNo << ", Name: " << name << endl;

};

int main() {

Student s[3];

for(int i = 0; i < 3; i++) {

cout << "\nEnter details for student " << i + 1 << ":\n";
s[i].getDetails();

cout << "\nDisplaying student details:\n";

for(int i = 0; i < 3; i++) {

s[i].displayDetails();

return 0;

}
Experiment 4
a. WAP for static data members

#include<iostream>

using namespace std;

class myclass {

public:

int x;

static int count;

myclass() //default constructor

count++;

};

// static data member initialization

int myclass::count = 0;

int main()

cout << "initial count: " << myclass::count << endl;

myclass m1;

cout << "count after one object: " << myclass::count << endl;

myclass m2;
cout << "count after two objects: " << myclass::count << endl;

return 0;

}
b. WAP for static member function

#include <iostream>

using namespace std;

class myclass {

static int count;

public:

myclass() { // default constructor

count++;

// static member function to return count

static int getcount() {

// return x; // would cause error since x is not static

return count;

};

// initialize static member

int myclass::count = 0;

int main() {

cout << "Initial value: " << myclass::getcount() << endl;

myclass m1, m2;


// Cannot access 'count' directly as it's private

// So we use static function to get its value

cout << "Count after two objects: " << myclass::getcount() << endl;

return 0;

You might also like