Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask C/C++ Expert


Home >> C/C++

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.

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M9508731

Have any Question?


Related Questions in C/C++

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As