ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP2111Classes And Objects
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP2111

    Object Oriented Programming

    Progress0 / 23 done
    Topics23
    1Introduction to object oriented design2History and advantages of object oriented design3Introduction to object oriented programming concepts4Classes and objects5Data encapsulation6Constructors and destructors7Access modifiers8Const vs non-const functions9Static data members & functions10Function overloading11Operator overloading12Identification of classes and their relationships13Composition and aggregation14Inheritance15Multiple inheritance16Polymorphism17Abstract classes and interfaces18Generic programming concepts19Function & class templates20Standard template library21Object streams22Data and object serialization using object streams23Exception handling
    2 min read
    Topic 4 of 23
    Topic Study Note Illustration
    04

    Classes and objects

    Classes and Objects

    Classes and Objects

    In Object-Oriented Programming, classes and objects are the basic building blocks. They help organize code in a way that reflects real-world things.


    Class

    A class is a blueprint or template used to create objects. It defines the properties (also called attributes or data members) and behaviors (also called methods or functions) that the object will have. However, a class by itself does not take up memory—it only defines the structure.

    Syntax Example (C++):

    class Car {
    public:
        string brand;
        int speed;
    
        void start() {
            cout << "Car has started" << endl;
        }
    
        void accelerate() {
            speed += 10;
        }
    };
    

    In this example, Car is a class with two data members (brand, speed) and two member functions (start(), accelerate()).


    Object

    An object is a real, usable instance of a class. When an object is created, memory is allocated for its data members. Each object has its own copy of the data but can use the functions defined in the class.

    Creating an Object (C++):

    Car myCar;  // myCar is an object of class Car
    myCar.brand = "Toyota";
    myCar.speed = 0;
    
    myCar.start();       // Calls start() function
    myCar.accelerate();  // Increases speed by 10
    

    Here, myCar is an object of the Car class. It has its own values for brand and speed and can perform actions like starting and accelerating.


    Key Points:

    • A class defines the structure and behavior.
    • An object is created from a class and is used in the actual program.
    • One class can be used to create many objects.
    • Each object operates independently with its own data.

    Real-Life Example:

    Think of a class as a recipe for baking a cake. The recipe tells you what ingredients are needed and the steps to follow. But the actual cake you bake using that recipe is the object. You can bake many cakes (objects) using the same recipe (class).

    This approach makes code organized, reusable, and easier to manage.

    ← Previous topic 3
    Introduction to object oriented programming concepts
    Next topic 5 →
    Data encapsulation

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    2mremaining
    335words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Object Oriented Programming