Home | | Computer Science 11th std | C++: Creating Objects

Example Programs in C++ - C++: Creating Objects | 11th Computer Science : Chapter 14 : Classes and objects

Chapter: 11th Computer Science : Chapter 14 : Classes and objects

C++: Creating Objects

A class specification just defines the properties of a class.

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.

 

For example

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

 

(1) Global 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

 

(2) Local Object

If an object is declared with in a function then it is called local object.

It cannot be accessed from outside the function.

 

Illustration 14.2 The use of local object

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;

}

Output:

151515

 

Tags : Example Programs in C++ , 11th Computer Science : Chapter 14 : Classes and objects
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 14 : Classes and objects : C++: Creating Objects | Example Programs in C++

Related Topics

11th Computer Science : Chapter 14 : Classes and objects


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.