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

Assignment Help:

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.


Related Discussions:- Specifying and working rules of a class

Give advantage of using nfs, Question: a) Give two uses of NFS. b) G...

Question: a) Give two uses of NFS. b) Give one advantage of using NFS. c) Explain the following lines as they would be interpreted in the /etc/exports file i. /usr/local

Program to calculate the average sales for each employee , There are 2...

There are 25 employees in a business. Each employee has 5 sales each day of the month (Assume there are 30 days in each month of the year). Draw a flowchart for a pr

Arrays, how to write the code for operation research

how to write the code for operation research

Memory management operator, Memory Management Operator In C malloc( ), ...

Memory Management Operator In C malloc( ), calloc( ), realloc( ), and free( ) are used to mange dynamic memory.  In addition to these function C++ have derived two unary ope

Big o notation, The probabilistic Hough transform uses random sampling inst...

The probabilistic Hough transform uses random sampling instead of an accumulator array. In this approach the number of random samples r, is not specified in the OpenCV call, but is

A Padovan string P(n) for a natural number n, c program for padovan string ...

c program for padovan string   Padovan series are positive integers obtained by the following process: The Padovan series is the sequence of integers P(n) defined by the

Described the scope resolution operator?, A: It allows a program to referen...

A: It allows a program to reference an identifier in global scope which has been hidden by another identifier along with the same name in the local scope.

Integer variable, how do you declare an integer variable

how do you declare an integer variable

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd