FAQ
Classes and Objects in C++, cpp, C Plus plus
Class
l A class is usually represents a
noun.
l It encapsulates the state and
behavior of the concept it represents.
l It encapsulates the state through
data placeholders called attributes(or member variables)
l It encapsulates behavior through
sections of code called methods (functions).
Object
l Object is an instance of class.l Object is a runtime entity
l Object is a physical representation of a class
An example of class
#include<iostream.h>
class distance
{
private:
int km,m,cm;
public:
void getdata()
{cout<<”Enter KM, M, CM:”;
cin>>km>>m>>cm;
}
void showdata()
{
cout<<”KM=”<<km<<”M=”<<m<<”CM=”<<cm;
}
};
void main()
{
distance d1,d2;
d1.getdata();
d2.getdata();
d1.show();
d2.show();
}
Access Specifiers
l There are three access specifiers: private,protected and public.l These access specifiers are used to categorize access rules for class members
l Those data members and member functions declared as private, can not accessed outside the class.
l Those data members and member functions declared as public can accessed outside the class along with inside access
l Protected works similar to private that is can not access from outside the class but the difference between private and protected will get clear in inheritance.
Properties of member functions
l Member variables and member functions can be accessed from outside class with the help of object of the same class and dot operator, only when members are public.
l Member functions can access other members of the same class regardless of their access category.
l Member functions can access other members without using object and dot operator. For example, in above program getdata() is accessing km,m and cm without using object and dot operator. These variables are members of object who invoked member function getdata().
l Member function can be defined inside or outside the class. If they are defined inside the class, mechanism is very clear from the above example, as we defined two member functions getdata() and showdata() inside the class.
l When we intend to define member functions outside the class, we have to declare it inside the class. When we define member function outside, membership label should be placed between return type and function name. So the syntax is
return type Class name :: Function name (argument list)
{… …...
}
l All functions defined inside the
class are inline by default. If definition of member function is written
outside the class then inline keyword must use to make function inline.
l Several different classes can use
the same function name. The membership label will resolve their scope.
Function call by
passing objects and returning objects
#include<iostream.h>class ComplexNumb
{
private:
int a;
int b;
public:
void getdata();
void showdata();
ComplexNumb add(ComplexNumb);
};
void ComplexNumb:: getdata()
{
cout<<”Enter two numbers”;
cin>>a>>b;
}
void ComplexNumb:: showdata()
{
cout<<”a=”<<a<<”b=”<<b;
}
ComplexNumb ComplexNumb :: add(ComplexNumb c)
{
ComplexNumb temp;
temp.a=a+c.a;
temp.b=b+c.b;
return(temp);
}
void main()
{
ComplexNumb c1,c2,c3;
c1.getdata();
c2.getdata();
c3=c1.add(c2);
c3.show();
}
We have created three objects of type ComplexNumb
We call getdata() twice first via c1 and then via c2.
Now member function add() is called via c1 and passes object c2 as a parameter.
In function add() value of c2 is received in object c.
We have created another object temp in function add() to temporary hold sum of c1 and c2.
Function add() returns object temp, this value is then get collected in object c3.
l Show() function is next used to
display content of c3
How to use Classes and Objects in C Plus Plus C++, Cpp
with Example
How to use Classes and Objects in C Plus Plus C++, Cpp
with Example
How to use Classes and Objects in C Plus Plus C++, Cpp
with Example
How to use Classes and Objects in C Plus Plus C++, Cpp
with Example
No comments:
Post a Comment