Specifying and working rules of a class, C/C++ Programming

Specifying a Class:

As discussed a class is defined to develop an algorithm and bind it together in a core shell.

A class is an abstract data type (ADT).  The binding of data members and members function in a class is called encapsulation.  The class is declared as shown.

class ece

{ private:

int x, float y;

void f_priv(int a, int b);

z =f_over(x,y); // The value of z is 0.5

public:

int r, float s;

void f_pub(float t, float u);

}

In the above example a class is defined and it is named ece.  The class ece has public and private variables and function prototypes.  The most striking feature of class is variables can be defined as public or private.

The  variables  inside  the class are  called data members  and  the  functions  are  called

member functions.

Basic rules of working with class:

  • Only the public members can access the private members of the same class directly without using any operators like (.).
  • The public members can be accessed outside the class using scope operators.
  • Member function can de defined inside the class without having the prototype, this style of creating a member function is similar to inline function and it has to follow the same rules of inline function. The key word inline is not necessary.
  • Member function can also be created outside the class by declaring the function prototype inside the class. The prototype name must be identical to the function definition name.
  • If the members of class are not defined with key word public or private than the members are defaulted as private member and thus all the members are hidden.
  • Private members cannot be accessed outside the class. Therefore the range and visibility of private member is within its class.

think about the following example to explain further about class.

class item

{ int number;

float cost;

public:

int z;

void getdata(int a, float b);

void putdata(void);

};

In  the  above  example  the  member  data  (variable)  number  and  cost  will  be  private member because default scope of member is private.  Where as member functions getdata and putdata are public members.  The function is declared as prototype.   Only these functions can access the private member data cost and number.

A class item has been defined. Now any number of objects of class item can be used with simple statement.

item x,y,z;   This statement will create three fresh objects of class item.   In other word object is a variable of type class.  When a class is defined memory is not allocated, but when an object is created required memory is allocated for that object.   Object can be declared at the definition of class but this method is not followed because objects are produced only at the time of necessity.

 

class item

{

float cost;

int number; public:

int z;

void getdata(int a, float b);

void putdata(void);

}x,y,z;

 

How to access the class members:

As discussed earlier when the class are used in the main( ) part of the program only

public members can be accessed the private members cannot be read even through any operator. It is hidden for ever and it is exclusively only by its different class members either private of public. On other hand the public members can be used outside the class.

The syntax of using the public data member and member function are: object_name.data_memeber; This is for public data member which is z. object_name.member_function  (argument);   This is for public member function which

are getdata and putdata.

By using above syntax the members can be accessed as follows:

item p;

p.z =10; The public data member z is assigned 10. p.getdata(10,11.22);

p.putdata( );

To hide the data usually the data member is not declared as a public member; on top of example z is public data member.   This kind of declaration of data member as public should be avoided and the follow the method given below.

 

class item

{ int number; float cost; public:

void getdata(int a, float b);

void putdata(void);

}x,y,z;

 

Defining Member functions:

A  member  function  can  be  declared  inside  the class  definition  or outside  the  class definition.

Outside the Class Definition:

A member  function  can  be  defined  outside  the class.   Before  defining  the  member

function outside the class a prototype of that member function should be declared in the class. There is a little difference between when defining the member function outside the class from regular function definition.   That is when declaring a member function outside the class the function must recognize for which class is this member function is defined.  There fore the syntax for defining the member function outside the class is;

return_type class_name : : function_name (argument)

{ statements;

}

Consider the following class:

class item

{ int number;

float cost;

public:

void getdata(int a, float b);

void putdata(void);

};

 

The class item has public member function prototype getdata and putdata.  This member functions can be defined outside the class as shown.

void item : : getdata(int a, float b)

{ number = a;

cost = b;

}

void item : : putdata(void)

{ cout <<"Number is " <

cout << "Cost is " << cost << "\n";

}

As discussed the public member can access the private member only of that class directly

without dot operator. In the above example the member functions are accessing the private data members' number and cost without referring to the class.  A private member cannot be accessed outside the class.  A similarly the member function can call the other member function of same class without any operator this is also called nesting of member functions.

 

Inside the Class Definition:

The member function can be defined inside the definition of class.   When a member function is defined inside the class it has all the properties of inline function, in other word it is also an inline function.

 

class item

{ int number; float cost; public:

void getdata(int a, float b);

 

void putdata(void)

{ cout <<"Number is " <

cout << "Cost is " << cost << "\n";

}

};

 

Inline function Outside the class definition:

inline void item : : getdata(int a, float b)

{ number = a;

cost = b;

}

Therefore an inline function can be created even outside the class definition.  Therefore it is always a good practice as a programmer to define function outside the class definition.

Posted Date: 9/20/2012 7:53:02 AM | Location : United States







Related Discussions:- Specifying and working rules of a class, Assignment Help, Ask Question on Specifying and working rules of a class, Get Answer, Expert's Help, Specifying and working rules of a class Discussions

Write discussion on Specifying and working rules of a class
Your posts are moderated
Related Questions
palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindromes

Create a program that asks the user for two integers which represent the numerator and denominator parts of fraction. Print out a simplification of the fraction. Example: Enter th

Write a program to calculate the average of ten numbers       #include stdio.h     /* Only this header is needed since only IO are used */     void main()     {          char

1. Using Visual C++ and your OpenGL configured environment, write an application that displays a “unique” graphical scene that you designed and coded for this course. What yo

smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

I need help in the C++ programming assignment. I can send the files the assignment is times 3hrs.

What is the purpose of return values? Can you not return any values from a function? If you could what would he function look like?

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

Changing Field width The printf() and scanf() functions from the standard library, use width specifier for controlling the width of its output . Similarly, the ostream class al

Selection sort - C program: Write a program to define a selection sort. void main()  {   clrscr();   int a[100],ch,n;   cout   cin>>n;   for (int i=0;i