Skip to content

Class

A class is a blueprint or a template used to create objects. It serves as a model that defines the properties (attributes) and behaviors (methods) that objects of that class will have.

Constructor

This is a method within a class that gets called automatically when an object of that class is created. It initializes the object's attributes or performs any setup required for the object to function properly.

Attributes

These represent the data or characteristics that each object of the class will possess. For example, in a Smartphone class, attributes could include brand, color, memory, screen size.

Methods

These are functions defined within the class that describe the actions or behaviors that the objects can perform. For instance, a Smartphone class might have methods like power on, power off, check memory, etc.

Declare Class

Class declaration involves specifying the blueprint or structure of an object in an object-oriented programming language.

Note

In C, a struct is used to define a data structure that can hold attributes similar to a class. This structure encapsulates related data items but doesn't have built-in methods associated with it.

c
// Syntax
struct ClassName {

};

// Example
struct SmartPhone {

};
c++
// Syntax
class ClassName {
 
};

// Example
class SmartPhone {

};
java
// Syntax
public class ClassName {

}

// Example
public class SmartPhone {

}
python
# Syntax
class ClassName:

# Example
class SmartPhone:

Define Attribute

Defining attributes within a class refers to specifying the data or characteristics that each object created from that class will possess.

Note

In Python, we don't explicitly declare access modifiers like private as in Java. By convention, attributes are considered as public, but their names may be prefixed with a single underscore to indicate that they are intended for internal use.

c
// Syntax
struct ClassName {
	//attributes
	data_type attribute_name;
};

// Example
struct SmartPhone {
    char brand[50];
    char color[20];
    int memory;  // in GB
    float screen_size;
};
c++
// Syntax
class ClassName {
	//attributes
  private:
    data_type attribute_name;
};

// Example
class SmartPhone {
  private:
    	std::string brand;
     std::string color;
     int memory;  // in GB
     float screen_size;
};
java
// Syntax
public class ClassName {
	//attributes
	data_type attribute_name;
}

// Example
public class SmartPhone {
    private String brand;
    private String color;
    private int memory;
    private float screenSize;
}
python
# Syntax
class ClassName:
    # Attributes
    attribute_name = None

# Example
class SmartPhone:
    def __init__(self):
        self.brand = ""
        self.color = ""
        self.memory = 0
        self.screen_size = 0.0

Define Methods

Defining a method involves creating a function within a class that describes the actions or behaviors an object can perform.

c
// Syntax
struct ClassName {
	//attributes
};

returnType methodName(struct ClassName *objectName) {
	// what to do
	// Return type specify the type of data that a function or method will return after its execution. Example: int, char, float, and void (does not return any value).
}

// Example
void powerOn(struct Smartphone *phone) {
    printf("Powering on the %s smartphone...\n", phone->brand);
}
c++
// Syntax
class ClassName {
        //attributes

    public:
        //methods
    return_type methodName() {
        // what to do
    }
};

// Example
class SmartPhone {
    //write attributes

    public:
        void powerOn() {
            std::cout << "Powering on the " << brand << " smartphone...\n";
        }
}
java
// Syntax
public class ClassName {
	//attributes

    //methods
    public return_type methodName() {
        //what to do
    }
};

// Example
public class SmartPhone {
	//write attributes

    public void powerOn() {
        System.out.println("Powering on the " + brand + " smartphone...");
    }
}
python
# Syntax
class ClassName:

    # Methods
    def method_name(self):
        # What to do

# Example
class SmartPhone:
    def power_on(self):
        print("Powering on the " + self.brand + " smartphone...")

Create Constructor

Creating a constructor involves defining a special method within a class that initializes the object's state when an instance of that class is created.

c
// Syntax
struct ClassName createClassName(data_type attribute) {
    struct ClassName object;
    strcpy(object.attribute, attribute); //if it’s a string
    object.attribute = attribute; //if numbers (int/float)
    return object;
}

// Example
struct Smartphone createSmartphone(char* brand, char* color, int memory, float screenSize) {
    struct Smartphone phone;
    strcpy(phone.brand, brand);
    strcpy(phone.color, color);
    phone.memory = memory;
    phone.screenSize = screenSize;
    return phone;
c++
// Syntax
class ClassName {
    //attributes
    //methods

    //constructor with parameters
    public:
        ClassName(data_type attributeName){
            this->attributeName = attributeName;
        }
};

// Example
public:
    Smartphone(std::string brand, std::string color, int memory, float screenSize) {
        this->brand = brand;
        this->color = color;
        this->memory = memory;
        this->screenSize = screenSize;
    }
java
// Syntax
    public class ClassName {
    //attributes
    //methods

    //constructor with parameters
    public Smartphone(data_type attributeName) {
            this.attributeName = attributeName;
    }
};

// Example
public Smartphone(String brand, String color, int memory, float screenSize) {
    this.brand = brand;
    this.color = color;
    this.memory = memory;
    this.screenSize = screenSize;
}
python
# Syntax
class ClassName:
    # Attributes

    # Constructor with parameters
    def __init__(self, attribute_name):
        self.attribute_name = attribute_name

# Example
class Smartphone:
    def __init__(self, brand, color, memory, screen_size):
        self.brand = brand
        self.color = color
        self.memory = memory
        self.screen_size = screen_size

Exercise

What to do?

  1. Make a class of Student, with these attributes:
    1. Name
    2. Age
    3. Height (in m)
    4. Gender (M/F)
    5. isActive (True/False)
    6. Student Number
    7. Year Enter
  2. Make some methods for the Student Class:
    1. To display name and age
    2. To calculate how long that person has been at the school
      Formula: currentYear - yearEnter
    3. To display whether the person an active student or not
      Output example:
      if active: name is an active student.
      if not active name currently is not an active student.