0% found this document useful (0 votes)
35 views10 pages

6 Semester Oop Lab Report 1

The document contains 6 code examples demonstrating OOP concepts in C++ including defining classes with member variables and functions, inheritance, polymorphism, and calculating areas and perimeters of shapes. Example classes include Circle, Rectangle, Triangle, Person, Box, and Shape.

Uploaded by

islamshofi942
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)
35 views10 pages

6 Semester Oop Lab Report 1

The document contains 6 code examples demonstrating OOP concepts in C++ including defining classes with member variables and functions, inheritance, polymorphism, and calculating areas and perimeters of shapes. Example classes include Circle, Rectangle, Triangle, Person, Box, and Shape.

Uploaded by

islamshofi942
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/ 10

/*A.

Write a program to implement a class called Circle that has private member variables for
radius.Include member functions to calculate the circle's area and circumference.*/

#include <iostream>

using namespace std ;

#include <cmath>

class Circle {

private:

double radius;

public:

Circle(double r) : radius(r) {}

double calculateArea() {

return M_PI * radius * radius;

double calculateCircumference() {

return 2 * M_PI * radius;

};

int main() {

double radiusValue;

cout << "Enter the radius of the circle: ";

cin >> radiusValue;

Circle myCircle(radiusValue);

cout << "Area of the circle: " << myCircle.calculateArea() << endl;

cout << "Circumference of the circle: " << myCircle.calculateCircumference() <<endl;

return 0;

}
/*B. Write a program to create a class called Person that has private member variables for name, age
and country. Implement member functions to set and get the values of these variables.*/

#include <iostream>

using namespace std ;

#include <string>

class Person {

private:

string name;

int age;

string country;

public:

void setPersonInfo(const string& n, int a, const string& c) {

name = n;

age = a;

country = c;

string getName() const {

return name;

int getAge() const {

return age;

string getCountry() const {

return country;

};

int main() {

Person person1;
person1.setPersonInfo("Shofikul ", 21, "Bangladesh");

cout << "Name: " << person1.getName() << endl;

cout << "Age: " << person1.getAge() << endl;

cout << "Country: " << person1.getCountry() << endl;

return 0;

/*C. Write a program that defines a shape class with a constructor that gives value to width and
height.

The define two sub-classes triangle and rectangle, that calculate the area of the shape area (). In the
main,

define two variables a triangle and a rectangle and then call the area() function in this two
variables.*/

#include <iostream>

using namespace std ;

class Shape {

protected:

double width;

double height;

public:

Shape(double w, double h) : width(w), height(h) {}

virtual double area() const = 0;

};

class Triangle : public Shape {

public:

Triangle(double base, double height) : Shape(base, height) {}


double area() const override {

return 0.5 * width * height;

};

class Rectangle : public Shape {

public:

Rectangle(double w, double h) : Shape(w, h) {}

double area() const override {

return width * height;

};

int main() {

Triangle myTriangle(5.0, 8.0);

cout << "Area of the Triangle: " << myTriangle.area() << endl;

Rectangle myRectangle(4.0, 6.0);

cout << "Area of the Rectangle: " << myRectangle.area() << endl;

return 0;

/* D. Write a program to declare class BOX

private
height, width, depth

public

declare 1 default constructor, 1 parameterized constructor and show() to display the values. Write
main()

function to declare an object to invoke all the constructor and show function for each object.*/

#include <iostream>

using namespace std ;

class Box {

private:

double height;

double width;

double depth;

public:

// Default constructor

Box() : height(0.0), width(0.0), depth(0.0) {}

// Parameterized constructor

Box(double h, double w, double d) : height(h), width(w), depth(d) {}

// Member function to display values

void show() const {

cout << "Height: " << height << " | Width: " << width << " | Depth: " << depth << endl;

};

int main() {

// Declare an object using the default constructor

Box defaultBox;
// Display values using the show() function

cout << "Default Constructor - ";

defaultBox.show();

// Declare an object using the parameterized constructor

Box paramBox(3.0, 4.0, 5.0);

// Display values using the show() function

cout << "Parameterized Constructor - ";

paramBox.show();

return 0;

/* E. Write a program to print the area and perimeter of a triangle having sides of 3, 4 and 5 units by
creating

a class named 'Triangle' with the constructor having the three sides as its parameters. */

#include <iostream>

using namespace std ;

#include <cmath>

class Triangle {

private:

double side1;

double side2;

double side3;

public:

Triangle(double s1, double s2, double s3) : side1(s1), side2(s2), side3(s3) {}


double calculateArea() const {

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

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

double calculatePerimeter() const {

return side1 + side2 + side3;

};

int main() {

Triangle myTriangle(3.0, 4.0, 5.0);

cout <<"Area of the Triangle: "<< myTriangle.calculateArea() << endl;

cout <<"Perimeter of the Triangle: "<< myTriangle.calculatePerimeter() <<endl;

return 0;

/* F. Write a program which will calculate the area of circle and rectangle using a base class shape and

derived classes circle and rectangle. */

#include <iostream>

using namespace std ;

#include <cmath>

class Shape {

public:

virtual double calculateArea() const = 0;


};

class Circle : public Shape {

private:

double radius;

public:

Circle(double r) : radius(r) {}

double calculateArea() const override {

return M_PI * radius * radius;

};

class Rectangle : public Shape {

private:

double length;

double width;

public:

Rectangle(double l, double w) : length(l), width(w) {}

double calculateArea() const override {

return length * width;

};

int main() {
Circle myCircle(5.0);

cout << "Area of the Circle: " << myCircle.calculateArea() <<endl;

Rectangle myRectangle(4.0, 6.0);

cout << "Area of the Rectangle: " << myRectangle.calculateArea() <<endl;

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy