a What is the difference between Classes and Structure in C
a) What is the difference between Classes and Structure in C++?
b) Explain the following with the help of a program in C++?
i) Class
ii) Object
iii) Constructor
c) What is a default constructor in c++?
Solution
a) Answer:
1) class members have the private access by default,
structure members have the public access by default.
b) Answer:
i) Class:
A class is a collection of data and methods.
A class is used to specify the collection of an object and it combines data and methods for manipulating that data into single package. The data and functions within a class are called members of the class.
A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations. For example, we defined the Box data type using the keyword class as follows:
ii) Object:
Object is a instance of class, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. Following statements declare two objects of class Box:
iii) Constructor:
A class constructor is a special member function of a class that is executed whenever objects of that class is created.
A constructor will have same name as the class and it does not have any return type at all.
Example:
Constructors can be defined inside the class definition or outside class definition using class name and scope resolution :: operator.
c)Default Constructor:
Default constructor is the constructor which doesn\'t take any argument. It has no parameter.
Example:
In above example, when the object is created the constructor is called which initializes its data members.
