Constructors are special class members that initialize objects and have the same name as the class, with no return type and can be default, parametrized, or copy constructors. Destructors, which also share the class name but are preceded by a tilde, automatically delete objects when they go out of scope. Static data members and functions are shared among all class objects, with static members being initialized to zero and accessible only to other static members.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views
Unit2Part2 (1)
Constructors are special class members that initialize objects and have the same name as the class, with no return type and can be default, parametrized, or copy constructors. Destructors, which also share the class name but are preceded by a tilde, automatically delete objects when they go out of scope. Static data members and functions are shared among all class objects, with static members being initialized to zero and accessible only to other static members.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7
Constructor
● Constructors are special class members which are called by the
compiler every time an object of that class is instantiated. ● Constructors have the same name as the class and may be defined inside or outside the class definition. How constructors are different from a normal member function? ● Constructor has same name as the class itself ● Constructors don’t have return type ● A constructor is automatically called when an object is created. ● If we do not specify a constructor, C++ compiler generates a default constructor for us (expects no parameters and has an empty body). ● They should always be declared in public section. ● They cannot be inherited. ● They can have default arguments. ● They cannot be virtual. Types of Constructors ● Default constructors: – Default constructor is the constructor which doesn’t take any argument. – It has no parameters. ● Parametrized constructors: – Is a constructor which can take arguments (which are passed when an object is created) to initialize the data members. ● Copy constructors: – It is a member function which initializes an object using another object of the same class. Destructor ● Destructor is a member function which destructs or deletes an object. ● A destructor function is called automatically when the object goes out of scope: – the function ends – the program ends – a block containing local variables ends – a delete operator is called ● Destructors have same name as the class preceded by a tilde (~) ● Destructors don’t take any argument and don’t return anything Static Data Members ● Static data members are class members that are declared using the static keyword. ● There is only one copy of the static data member in the class, even if there are many class objects. ● This is because all the objects share the static data member. ● The static data member is always initialized to zero when the first class object is created. Static Member Functions ● We can also declare the member function as static. ● Static member functions have following properties: – They can have access to only other static members declared in same class. – They can be called using class name.