Page 124 - DCAP404 _Object Oriented Programming
P. 124
Unit 6: Constructors and Destructors
{ Notes
int x,y;
public:
abc(int, int);
}
abc::abc(int a, int b)
{
x=a;
y=b;
}
Task ‘The constructor also usually holds the initializations of the different declared
member variables of its object’. Explain this statement.
Observed that myabc class has now a constructor defined to except two parameters of integer
type. We can now create an object of myabc class passing two integer values for its construction,
as listed below:
main()
{
abc myabc(100,200);
———;
}
In the main function myabc object is created value 100 is stored in data variable x and 200 is
stored in data variable y. There is another way of creating an object as shown below.
main()
{
myabc=abc(100,200);
———;
}
Both the syntaxes for creating the class are identical in effect. The choice is left to the programmer.
There are other possibilities as well. Consider the following class differentials:
class abc
{
int x,y;
public:
abc();
}
LOVELY PROFESSIONAL UNIVERSITY 117