ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Object Oriented Programming
    COMP2111
    Progress0 / 23 topics
    Topics
    1. Introduction to object oriented design2. History and advantages of object oriented design3. Introduction to object oriented programming concepts4. Classes and objects5. Data encapsulation6. Constructors and destructors7. Access modifiers8. Const vs non-const functions9. Static data members & functions10. Function overloading11. Operator overloading12. Identification of classes and their relationships13. Composition and aggregation14. Inheritance15. Multiple inheritance16. Polymorphism17. Abstract classes and interfaces18. Generic programming concepts19. Function & class templates20. Standard template library21. Object streams22. Data and object serialization using object streams23. Exception handling
    COMP2111›Classes and objects
    Object Oriented ProgrammingTopic 4 of 23

    Classes and objects

    2 minread
    335words
    Beginnerlevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time2 min
      Word count335
      Code examples0
      DifficultyBeginner