You are on page 1of 7

NATIONAL INSTITUTE OF

TECHNOLOGY

JALANDHAR, PUNJAB
OBJECT ORIENTED PROGRAMMING
LAB

SESSION:2022-26

SUBMITTED BY:
NAME: BHUMIKA

ROLL NO: 22103041

BRANCH: CSE

SUBMITTED TO: RAJNEESH RANI


LAB 5
Program 1: Make a class triangle with following functions:

a) Get its three sides from the user using getdata() function. (The function must be defined
outside

the class)

b)Calculate its area(The function must be defined inside the class)

c)Display the area(The function must be defined outside the class, but made inline)

Demonstrate the above program using three objects

#include <iostream>

using namespace std;

class Triangle {

private:

double side1, side2, side3;

public:

void getData();

double calculateArea() {

double s = (side1 + side2 + side3) / 2;

return std::sqrt(s * (s - side1) * (s - side2) * (s - side3));

};

void Triangle::getData() {

std::cout << "Enter the lengths of the three sides of the triangle: ";

std::cin >>side1>>side2 >>side3;

inline void displayArea(double area) {

std::cout << "The area of the triangle is: " << area << std::endl;
}

int main() {

Triangle triangle1, triangle2, triangle3;

std::cout << "Enter data for triangle 1:" << std::endl;

triangle1.getData();

double area1 = triangle1.calculateArea();

displayArea(area1);

std::cout << "Enter data for triangle 2:" << std::endl;

triangle2.getData();

double area2 = triangle2.calculateArea();

displayArea(area2);

std::cout << "Enter data for triangle 3:" << std::endl;

triangle3.getData();

double area3 = triangle3.calculateArea();

displayArea(area3);

return 0;

OUTPUT
Program 2: Make the student class with

a)data members-rollno, name, marks of three subjects, total marks

b) member functions-setdata() – To set the values of objects

- total ()- To find the total marks

-display_data()- To display all the details and total marks (Nest total()
function in this function)

#include <iostream>

#include <string>

class Student {

private:

int rollno;

std::string name;

int marks[3];

int totalMarks;
public:

void setData(int r, std::string n, int m1, int m2, int m3) {

rollno = r;

name = n;

marks[0] = m1;

marks[1] = m2;

marks[2] = m3;

int calculateTotal() {

totalMarks = marks[0] + marks[1] + marks[2];

return totalMarks;

void display_Data() {

scout << "Roll No: " << rollno;

cout << "\nName: " << name;

cout << "\nMarks in Subject 1: " << marks[0];

cout << "\nMarks in Subject 2: " << marks[1];

cout << "\nMarks in Subject 3: " << marks[2];

cout << "\nTotal Marks: " << calculateTotal();

};

int main() {

Student student1;

student1.setData(22103001, "Arti", 85, 90, 78);

student1.display_Data();

return 0;

}
OUTPUT

Program 3: WAP that create class BankAccount to manage bank accounts. Each account has
an account number and a balance. Implement the following requirements using static data
members and static member functions:

1) The BankAccount class should have a static data member totalAccounts to keep track of
the total number of bank accounts created.

2)The BankAccount class should have a static member function getTotalAccounts() that
returns the total number of bank accounts created.

3)Each time a new account is created, the totalAccounts count should be updated.

#include<iostream>

using namespace std;

class bankaccount{

private:

int account_no;

double balance;

static int totalaccounts;

public:

bankaccount(int accountnumber,double intibalance){

account_no=accountnumber;

balance=intibalance;

totalaccounts++;
}

static int gettoalaccounts(){

return totalaccounts;

};

int bankaccount::totalaccounts=0;

int main(){

bankaccount account_no1(1001,5000.0);

bankaccount account_no2(1002,7890.5);

cout<<"Total bank accounts created:"<<bankaccount::gettoalaccounts();

return 0;

OUTPUT

You might also like