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 PortalCOMP2111Constructors Destructo…
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 6 of 23
    Topic Study Note Illustration
    06

    Constructors and destructors

    Constructors and Destructors

    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 view and download past papers

    Click on Show Past Papers to load available exam papers.

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

    No headings found

    Study Toolkit

    Subject Portal

    Object Oriented Programming