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

Write a program to calculate the total resistance, Write a program to calcu...

Write a program to calculate the total resistance of a series or parallel circuit. The maximum number of resistors is two.   We need to decide whether the user wants the to

C program for swapping the string, C Program for SWAPPING THE STRING #i...

C Program for SWAPPING THE STRING #include conio.h> #include stdio.h> void main() {           char a[50],temp=0;           int i=0,c=0,c1=0,l=0;           clr

Program, Modify the FACTOR program in this chapter so that it repeatedly as...

Modify the FACTOR program in this chapter so that it repeatedly asks for a number and calculates its factorial, until the user enters 0, at which point it terminates. You can enclo

Define classes and objects, Classes and Objects A class is a vehicle to...

Classes and Objects A class is a vehicle to execute the OOP features in the C++ language. Once a class is declared, an object of that type can be explained. An object is said t

Calculate the average assignment grade, 1. The main program must be in a fi...

1. The main program must be in a file called A4.cpp 2. The data must be read in from a data file.  The user must enter the filename.  A sample data file will be provided on Mood

Solve, solve for radius(R) of a circle of its area is to be inputted by a u...

solve for radius(R) of a circle of its area is to be inputted by a user. also display compute the circle diameter hint: area= pir21 pi=2.1416

Area, write a program to find area of curve y=f(x) between x and x=b,integr...

write a program to find area of curve y=f(x) between x and x=b,integrate between the limits a and b using c     #include float start_point, /* GL

C program to demonstrate pointer to array, C program to demonstrate Pointer...

C program to demonstrate Pointer to array: void p2a(int *); void main() {                 int x=10, *a,**b;                 int arr[5];                 a=&x;//po

Develop a biomedical imaging project, Develop a biomedical imaging project ...

Develop a biomedical imaging project Project Description: This is a biomedical imaging project. Skills required are C++ Programming, Cocoa, Mac OS, Objective C

Modified distribution method for solving transportation prob, i want a c or...

i want a c or c++ code for solving transportation problem using modified distribution method

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