Thursday 25 August 2011

classes and objects


CLASS:
1.      A class serves as a blueprint or a plan or a template. It specifies what data and what functions will be included in objects of that type.
2.      A class is a collection of variables of different data types and functions.
3.      Most languages offer primitive data types like int, long and float. The programmer often needs to create his own data types; and this can be done with the help of a class.
4.      Class is a user defined data type.
5.      A class is tool for Data Abstraction.
6.      A class is created with a keyword “class”.
7.      A class is similar to that of a structure but all the members of a class are private by default. But, the members of structures are public by default.

SYNTAX:
        Class <class name>
         {
          Private:
                    Data-members;
                    Member-functions;
          Protected:
                    Data-members;
                    Member-functions;
           Public:
                   Data-members;
                   Member-functions;
           };

 EXAMPLE:
 class shape
{
private:
int l, b;
public:
void area ()
{
cout<<”area of a rectangle is:” l*b;
}
};
In the above program shape is a class. The variables l, b are private and the member function area( ) is public. Every class must terminate with a semicolon.
OBJECT:
An object is an instance of the class. It is an entity that has an existence.
The basic idea behind object oriented language is to combine into a single unit both data and  functions that operate on the data. Such a unit is called an “object”. For example, a fiat car with registration number AP 09D 3233 is a particular instance of the class cars. It has a unique identity. A car with a different registration number is a different object of the same class cars.
An object fundamentally has three characteristics:
1.      A State
2.      A Behavior
3.      An identity

For example, car is an object of the class cars. Where each car can have the following features:
State: Color, Size, Weight, Engine Capacity etc;
Behavior: Start, Stop, Accelerate etc.
Identity: Registration number etc.
HOW TO CREATE OBJECTS:
class shape
{
private:
int l, b;
public:
void area ()
{
cout<<”area of a rectangle is:” l*b;
}
};

Objects are declared in the similar way how variables are declared.
shape s1, s2;      //s1 and s2 are objects of type Shape

IMPORTANT FEATURES OF OBJECT:
1.      Each object will be having its own copy of member variables.
2.      But only one copy of member functions is maintained for all the objects of the class.
3.      The size of an object is equal to the sum of the sizes of its member variables.
4.      Member functions add nothing to the size of objects of the Class. Only member variables add to the size of objects of the class.
CLASS MEMBERS:
Class members include member variables or data variables and member functions or methods.
1.      MEMBER VARIABLES:
                          The variables that are declared inside the class are called as member variables. 
2.      MEMBER FUNCTIONS:
                          The functions that are defined inside the class are called as member functions. Member functions provide controlled access to the data members of a class. The difference between a method and a function is that function defined inside a class is a method.
The members of a class can be accessed by using the object of that class and dot (.) operator.

more about member functions and
types of member function member functions
 
EXAMPLE:
class Distance
{
private:
int feet;                                           //Member Variables
float inches;
public:
void setFeet(int x)                          //Member Functions
{
feet = x;
}
void setInches(float y)
{
inches = y;
}
void getFeet()
{
return  x;
}
void getInches()
{
return y;
}

};

void main()
{
distance d1, d2;       //Creating Objects
d1.setFeet(2);           //Member functions can be called with respect to the objects
                                   of the same class
d1.setInches(2.2);
d2.setFeet(3);
d2.setInches(3.2);
d1.feet++;                //Error. Private members can not be accessed outside the class 
cout<<d1.getFeet() <<d2.getFeet();
}


METHOD SIGNATURE
Signature of a function means number, sequence and type of formal arguments of the function.
1.      In method signature there is no return type.
2.      Just based on return type, we cannot distinguish the functions and methods.
3.      If two functions have given same name and also return type, then by the use of method signature we can distinguish between them, depending upon the type of parameters that are passed to the function call.
 EXAMPLE:
#include<iostream.h>
#include<conio.h>
int sum(int, int );   // {2, int, int} is the method signature of this method
int sum(int ,int ,int); // { 3, int, int, int} is the method signature of this method
void main()
{
int x,y;
x=sum(50, 60);  
y=sum(90, 80,70);
cout<<x<<endl<<y;
}
int sum(int a,int b)
{
return (a+b);
 }
int sum(int a, int b, int c)
{
return (a+b+c);
}
Output:
110
240

FUNCTION PROTOTYPING

A function prototype is a declaration that defines both: the type of arguments passed to the function and the return type of the function. A prototype describes the functions interface to the compiler. It tells the compiler the return type of the function as well as the number, type, and the sequence of its formal arguments.
It is important to notice that all C++ functions must be prototyped.
SYNTAX:
return_type function_name(arg_list);
For example
int add(int, int);
Providing names to the formal arguments in function prototypes is optional.
The following example makes it clear:
#include <iostream.h>
class Shape
{
int len, br;
void setval(int, int);   // function prototype
void display();           // function prototype
};

void Shape::setval(int c, int d) // :: is Scope resolution operator.
{
len=c;
br=d;
}

void Shape::display() // :: is Scope resolution operator. 
{
cout<< “Length “ << len’;
cout << “Breadh “ << br ;
}

void main()
{
Shape s1;
s1.setval(10,20);
s1.display();
}


types of the classes


2. anonymous class

ACCESS SPECIFIERS

What is singleton class?

No comments:

Post a Comment