Creating
Objects
A
class specification just defines the properties of a class. To make use of a
class specified, the variables of that class type have to be declared. The
class variables are called object.
Objects are also called as instance of class.
student s;
In
the above statement s is an instance
of the class student.
Objects
can be created in two methods,
(1)
Global object
(2)
Local object
If
an object is declared outside all the function bodies or by placing their names
immediately after the closing brace of the class declaration then it is called
as Global object. These objects can be used by any function in the program
If
an object is declared with in a function then it is called local object.
It
cannot be accessed from outside the function.
A
global object can be declared only for global class. If a class definition is
specified outside the body of all functions in a program then it is called
global class
#include <iostream>
#include <conio>
using namespace
std class add //Global class
{
int a,b;
public:
int sum;
void getdata()
{
a=5;
b=10;
sum = a+b;
}
} a1;
add a2;
int main()
{
add a3; //Local object for a global class
a1.getdata();
a2.getdata();
a3.getdata(); //public
data member accessed from outside the class
cout<<a1.sum;
cout<<a2.sum;
cout<<a3.sum;
return 0;
}
151515
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.