0% found this document useful (0 votes)
13 views

Classes

OOP Classes

Uploaded by

M Umar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Classes

OOP Classes

Uploaded by

M Umar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

What is a Class?

In C++, a class is like a blueprint or a template for creating objects. Think of it as a recipe that
tells you what ingredients (data) and steps (functions) are needed to create something. In
programming, these "ingredients" are called attributes (or data members), and the "steps" are
called methods (or member functions).

Basic Structure of a Class


A class typically has two main parts:
1. Attributes: These are the variables that hold the data or state of an object.
2. Methods: These are the functions that define the behavior or actions the object can
perform.
Here’s a simple example of a class in C++:
#include <iostream>
using namespace std;

class Car {
// Attributes (also called data members)
string brand;
string model;
int year;

public:
// Constructor (a special method to initialize objects)
Car(string b, string m, int y) {
brand = b;
model = m;
year = y;
}
// Method to display car details
void displayDetails() {
cout << "Brand: " << brand << "\nModel: " << model << "\nYear: " << year << endl;
}
};
Breaking It Down
1. Class Declaration:
o We start by using the class keyword followed by the class name, which is Car in
this case. The class name should start with an uppercase letter by convention.
2. Attributes:
o Inside the class, we declare the attributes. Here, brand, model, and year are
attributes of the Car class.
o These attributes are declared as private by default, meaning they cannot be
accessed directly from outside the class.
3. Constructor:
• A constructor is a special method with the same name as the class. It initializes the
attributes when an object is created.
• In this example, the constructor Car(string b, string m, int y) sets the brand, model, and
year attributes using the values passed to it.
4. Methods:
• The displayDetails method is a public function that prints out the details of the car. The
public keyword means this method can be accessed from outside the class.

Creating and Using Objects

• Once you have defined a class, you can create objects from it. Each object is an instance
of the class, with its own set of attributes.
• Here’s how you can create and use objects of the Car class:

What Happens Here?


• Object Creation: We create two objects, car1 and car2, using the Car class. Each object
has its own set of brand, model, and year.
• Calling Methods: We call the displayDetails() method on each object to print out its
details.

Types of classes:
We come across many different types of classes
• Abstract
• Sealed
• Static

Abstract Class

An abstract class is a class that cannot be instantiated directly. It contains at least one pure
virtual function (a function with no implementation), which must be implemented in a derived
class.

#include <iostream>

// Abstract class

class Animal {

public:

virtual void makeSound() = 0; // Pure virtual function

};

// Derived class that implements the abstract method

class Dog : public Animal {

public:

void makeSound() override {

std::cout << "Dog barks!" << std::endl;

};
int main() {

// Animal animal; // Error: cannot instantiate abstract class

Dog dog;

dog.makeSound(); // Output: Dog barks!

return 0;

• Key Property: Cannot create an object of the abstract class.


• Purpose: To provide a base for other classes with at least one method that must be
defined by child classes.

2. Sealed Class
A sealed class is a class that cannot be inherited from. In C++, this can be done using the final
specifier.
Example:
#include <iostream>
class Vehicle final { // 'final' prevents inheritance
public:
void drive() {
std::cout << "Driving the vehicle!" << std::endl;
}
};

// This will cause a compile-time error


// class Car : public Vehicle { };

int main() {
Vehicle vehicle;
vehicle.drive(); // Output: Driving the vehicle!
return 0;
}
• Key Property: Cannot be inherited by any other class.
• Purpose: To stop further inheritance and modifications from other classes.

Static Class

You can create a class where all members are static, meaning the class is not intended to be
instantiated, and its members belong to the class itself rather than an object.

#include <iostream>
class MathHelper {
public:
static int add(int a, int b) {
return a + b;
}
static int multiply(int a, int b) {
return a * b;
}
};
int main() {
// No need to create an instance of MathHelper
std::cout << "Addition: " << MathHelper::add(3, 4) << std::endl; // Output: 7
std::cout << "Multiplication: " << MathHelper::multiply(3, 4) << std::endl; // Output: 12
return 0;
}
• Key Property: All members are static, meaning they can be accessed without creating an
instance of the class.
• Purpose: To group utility functions or data that belong to the class rather than individual
objects.

You might also like