Oop Programs
Oop Programs
void introduce() {
cout << "My name is " << name << " and I am " << age << " years
old." << endl;
}
};
int main() {
// Creating an object of the "Person" class
Person person1;
return 0;
}
class Rectangle {
private:
int length;
int width;
public:
Rectangle(int l, int w) {
length = l;
width = w;
}
int area() {
return length * width;
}
};
int main() {
Rectangle rect(5, 3); // Creating an object of the Rectangle class
with parameters
cout << "Area of the rectangle: " << rect.area() << endl;
return 0;
}
class Circle {
private:
double radius;
public:
Circle(double r) {
radius = r;
}
double getArea() {
return 3.14159265 * radius * radius;
}
double getCircumference() {
return 2 * 3.14159265 * radius;
}
};
int main() {
Circle circle(4.0);
cout << "Area of the circle: " << circle.getArea() << endl;
cout << "Circumference of the circle: " << circle.getCircumference()
<< endl;
return 0;
}
class Counter {
private:
static int count;
public:
Counter() {
count++;
}
int Counter::count = 0;
int main() {
Counter c1, c2, c3;
cout << "Count: " << Counter::getCount() << endl;
return 0;
}
class Person {
private:
string name;
int age;
public:
Person() {
name = "John";
age = 30;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Person p;
p.display();
return 0;
}
#include <iostream>
using namespace std;
class Book {
private:
string title;
public:
Book(string t) {
title = t;
}
void displayTitle() {
cout << "Title: " << title << endl;
}
};
int main() {
Book b("The Catcher in the Rye"); // Calling the constructor to
initialize the object
b.displayTitle(); // Calling displayTitle() outside the class
return 0;
}
Destructor:
#include <iostream>
using namespace std;
class SimpleClass {
public:
// Constructor
SimpleClass() {
cout << "Constructor called" << endl;
}
// Destructor
~SimpleClass() {
cout << "Destructor called" << endl;
}
};
int main() {
cout << "Creating an object..." << endl;
SimpleClass obj; // Creating an object of SimpleClass
Example 8:
#include<iostream>
using namespace std;
class Employee{
private:
int a, b, c;
public:
int d, e;
void setData(int a, int b, int c); // Declaration
void getData(){
cout<<"The value of a is "<<a<<endl;
cout<<"The value of b is "<<b<<endl;
cout<<"The value of c is "<<c<<endl;
cout<<"The value of d is "<<d<<endl;
cout<<"The value of e is "<<e<<endl;
}
};
void Empolyee :: setData(int a1, int b1, int c1){
a=a1;
b=b1;
c=c1;
}
int main(){
Empolyee ahad;
ahad.a=135;
ahad.d=67;
ahad.e=87;
ahad.setData(1,2,4);
ahad.getData();
return 0;
}
Example 9:
#include<iostream>
using namespace std;
class Box{
public:
double length;
double breadth;
double height;
};
int main(){
Box Box1;
Box Box2;
double volume=0.0;
// Box 1 specification
Box1.breadth=7.0;
Box1.height=5.0;
Box1.length=6.0;
// Box 2 specification
Box2.breadth=14.0;
Box2.height=11.0;
Box2.length=13.0;
// Volume of box 1
volume=Box1.breadth*Box1.height*Box1.length;
cout<<"Volume of Box1 :"<<volume<<endl;
// Volume of Box 2
volume=Box2.breadth*Box2.height*Box2.length;
cout<<"Volume of Box2 :"<<volume<<endl;
return 0;
}