Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Oop Programs

The document provides 9 examples of using different object-oriented programming concepts in C++ like creating classes and objects, constructors, static members, default constructors, destructors, member functions, and calculating volumes of objects. The examples demonstrate how to define classes with data members and methods, initialize objects, access object properties and methods, define private and public members, and calculate values like areas and volumes. Constructors are shown being used to initialize objects, destructors to clean up memory, and static members to count object instances. Methods are demonstrated being called on objects from within and outside of classes.

Uploaded by

Muhammad Ahad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Oop Programs

The document provides 9 examples of using different object-oriented programming concepts in C++ like creating classes and objects, constructors, static members, default constructors, destructors, member functions, and calculating volumes of objects. The examples demonstrate how to define classes with data members and methods, initialize objects, access object properties and methods, define private and public members, and calculate values like areas and volumes. Constructors are shown being used to initialize objects, destructors to clean up memory, and static members to count object instances. Methods are demonstrated being called on objects from within and outside of classes.

Uploaded by

Muhammad Ahad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

OOP PROGRAMS

Example 1: Creating a Class and an Object


#include <iostream>
using namespace std;

// Declaration of a class named "Person"


class Person {
public:
string name;
int age;

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;

// Assigning values to the object's data members


person1.name = "Ahmad";
person1.age = 20;

// Calling the "introduce" method using the object


person1.introduce();

return 0;
}

Example 2: Class with Constructors


#include <iostream>
using namespace std;

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;
}

Example 3: Class with Data Members and Methods


#include <iostream>
using namespace std;

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;
}

Example 4: Static Members in a Class


#include <iostream>
using namespace std;

class Counter {
private:
static int count;
public:
Counter() {
count++;
}

static int getCount() {


return count;
}
};

int Counter::count = 0;

int main() {
Counter c1, c2, c3;
cout << "Count: " << Counter::getCount() << endl;
return 0;
}

Example 5: Class with Default Constructor


#include <iostream>
using namespace std;

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;
}

Example 6: Inside and Outside Member Function Calls


with Constructors

#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

cout << "Exiting the program..." << endl;


return 0; // The destructor will be automatically called when 'obj' goes out
of scope
}

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;
}

You might also like