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›Constructors and destructors
    Object Oriented ProgrammingTopic 6 of 23

    Constructors and destructors

    2 minread
    347words
    Beginnerlevel

    Constructors and Destructors

    In C++ Object-Oriented Programming, constructors and destructors are special functions used to manage how objects are created and destroyed. They are automatically called when an object is created or deleted.


    Constructor

    A constructor is a special member function that is called automatically when an object is created. It is used to initialize the object’s data members.

    Key Points:

    • Has the same name as the class
    • No return type, not even void
    • Can be overloaded (you can have multiple constructors with different parameters)

    Types of Constructors:

    1. Default Constructor
      Takes no arguments. Used to assign default values.

      class Student {
      public:
          int age;
          Student() {
              age = 18;
          }
      };
      
    2. Parameterized Constructor
      Takes arguments to initialize data with custom values.

      class Student {
      public:
          int age;
          Student(int a) {
              age = a;
          }
      };
      
    3. Copy Constructor
      Initializes an object using another object of the same class.

      class Student {
      public:
          int age;
          Student(int a) { age = a; }
          Student(const Student &s) {
              age = s.age;
          }
      };
      

    Destructor

    A destructor is a special member function that is called automatically when an object is destroyed. It is used to release memory or clean up resources used by the object.

    Key Points:

    • Has the same name as the class, but with a tilde (~) in front
    • Takes no arguments
    • Has no return type
    • Cannot be overloaded

    Example:

    class Student {
    public:
        Student() {
            cout << "Constructor called" << endl;
        }
    
        ~Student() {
            cout << "Destructor called" << endl;
        }
    };
    

    When an object of Student is created, the constructor runs automatically. When the object goes out of scope or is deleted, the destructor runs.


    Why Use Constructors and Destructors:

    • Constructors make sure objects are set up properly when they are created.
    • Destructors make sure resources like memory, files, or database connections are cleaned up when the object is no longer needed.

    They help in writing efficient and safe code, especially when dealing with dynamic memory or external resources.

    Previous topic 5
    Data encapsulation
    Next topic 7
    Access modifiers

    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 count347
      Code examples0
      DifficultyBeginner