Concept of classes and objects run side by side.Hence,its beneficial if we discuss the duo together.

A class is an extended prospect of a data structure : rather than keeping just data , it may possess both data and functions .
An object is an instantiation of a class . When it comes to variables , a class can be the type , as well as an object would be the variable . 


Format for declaring a class:

class class_name {
  access_specifier_1:
    member1;
  access_specifier_2:
    member2;
  ...
  } object_names;

Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the declaration can contain members, that can be either data or function declarations, and optionally access specifiers.
An access specifier is one among the following three keywords : private , public or protected . All these specifiers alter the access rights that the members following them acquire :
  • private members of a class are accessible just from within other members of the similar class or from their friends . 
  • protected members are accessible from members of their similar class and from their friends , as well as from members of their derived classes . 
  • Finally , public members are accessible everywhere where the object is visible .

By default, all members of a class declared with the class keyword have private access for all its members. 

Simple example showing classses:



#include <iostream>
#include<conio.h>
using namespace std;

// Class Declaration
class abc
{
//Access - Specifier
public:

//Varibale Declaration
  string a;
  int n;
};

//Main Function
int main()
{
    // Object Creation For Class
    abc obj;

    //Get Input Values For Object Varibales
    cout<<"Enter a :";
    cin>>obj.a;

    cout<<"Enter  n :";
    cin>>obj.n;

    //Show the Output
    cout << obj.a << ": " << obj.n << endl;

    getch();
    return 0;
}

Static storage classes:

The static storage class guides the compiler to retain a local variable in existence throughout the duration of the program in place of generating and eliminating it every time itarrives into and dies of scope . Thus , producing local variables static enables them to sustain their values between function calls . The static modifier are often applied to universal variables . If this is done , it will cause that variable's scope to be limited to the file wherein it is declared . 

In C++ , whenever static is employed on a class data member , it leads to only one copy of this member to be shared by all objects of its class .

Simple program showing static classes:


#include <iostream>
using namespace std;

class Yummy {

  public:
    static int a;
    Yummy () { a++; };
    ~Yummy () { a--; };
};

int Yummy::a=0;


int main () {

  Yummy m;
  Yummy n[5];
  Yummy * f = new Yummy;
  cout << m.a << endl;
  delete f;
  cout << Yummy::a << endl;
  return 0;
}

Extern Storage classes:

The extern storage class is employed to provide a reference of a global variable which is visible to ALL the program files . By using 'extern' the variable simply cannot beinitialized since all it does is aim the variable name at a storage area location which has been previously defined .

If you have numerous files and you define a global variable or function which can be used in additional files also , then extern will be utilized in another file to offer reference of defined variable or function . Only for understanding extern is employed to declare a global variable or function in additional files . The extern modifier is most typically used whenever there are two or more records sharing the identical global variables or functions.


0 comments to "CLASSES AND OBJECTS"

Post a Comment

Powered by Blogger.