Page 1 of 1

C++ BEGINNER BARE BONES CLASS TUTORIAL PART II C++ BEGINNER BARE BONES CLASS TUTORIAL PART II Rate Topic: ****- 1 Votes

#1 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 102
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 23 July 2009 - 11:53 AM

C++ BEGINNER BARE BONES CLASS TUTORIAL PART II


CONTENTS:
• I. ASSUMPTIONS
• II. INTRODUCTION
• III. OBJECTS
• IV. DATA MEMBERS
• V. SCOPE
• VI. PARAMETERS AND ARGUMENTS
• VII. MAIN FUNCTION

WHAT YOU WILL LEARN IN THIS TUTORIAL

1. You will learn how to instantiate or declare a class object.

2. You will learn how to keep the data members of a class private.

3. You will learn how to create public functions known as accessor methods.

4. You will learn how to successfully implement function prototype scope.

5. You will learn the difference between parameters and arguments.

• I. ASSUMPTIONS

I assume you have a C++ compiler and you want to learn more about C++ classes.

• II. INTRODUCTION

Hello; nice to meet you! Welcome to the “C++ Beginner Bare Bones Class Tutorial Part II.”

Please copy and paste the attached program into a file your compiler can read. Then build, compile, and debug the program. Following along, back and forth, between the tutorial and your copy of the program will help you understand the program functionality.


//***************************************************
//C++ Beginner Bare Bones Class Tutorial Part II
//With a sphere class object's data member variables example.
//***************************************************
#include <cmath>
#include <iomanip>
#include <iostream>

using namespace std;

const double PI = 3.14159;

//************************************
// Class CSphere
//************************************
class CSphere
{
	public:
			   CSphere();
		void   m_getRadiusSphere();			  //Radius of circle.
		void   m_DiameterSphere();			   //Diameter of circle.
		void   m_AreaCircle();				   //Area of circle.
		void   m_CircumferenceSphere();		  //Circumference of circle.
		void   m_VolumeSphere();				 //Volume of sphere.
		void   m_DisplayPublicSphere();		  //Dispalys properties of sphere.		 
			   ~CSphere();

	private:

		double a;  //Area of circle.
		double c;  //Circumference of circle.
		double d;  //Diameter of circle.
		double r;  //Radius of circle.
		double v;  //Volume of sphere.
};

//************************************
//Constructor of sphere class. 
//************************************
CSphere::CSphere()  
{
	
}

//************************************
//Radius of circle. 
//************************************
void CSphere::m_getRadiusSphere()  
{
	cout << endl << endl << endl;
	cout << "  C++ BEGINNER BARE BONES CLASS TUTORIAL PART II" << endl;
	cout << "  ----------------------------------------------" << endl << endl;
	cout << "  Please enter radius of sphere." << endl << endl;
	cout << "  For example; 1 and then press Enter ==> ";
	cin >> r;						//Obtain value for Radius.
}

//************************************
//Computes diameter of circle, equals
//d = (r * 2); 
//************************************
void CSphere::m_DiameterSphere()  
{
	d = (r * 2);
}

//************************************
//Computes area of circle, equals
//a = (PI * pow(Radius, 2)); 
//************************************
void CSphere::m_AreaCircle() 
{
	a = (PI * pow(r, 2));
}

//************************************
//Computes circumference of circle, equals
//c = (2*PI*Radius); 
//************************************
void CSphere::m_CircumferenceSphere()  
{
	c = (2 * PI * r);
}

//************************************
//Computes volume of a sphere, equals
//v = (((4/3) * PI) * pow(Radius, 3)); 
//************************************
void CSphere::m_VolumeSphere()   
{
	v = (((4 / 3) * PI) * pow( r, 3 ));
}

//************************************
//Displays properties of sphere. 
//************************************
void CSphere::m_DisplayPublicSphere()  
{

	cout << endl << endl << endl;
	cout << "	 C++ BEGINNER BARE BONES CLASS TUTORIAL PART II   " << endl;
	cout << "  ----------------------------------------------------" << endl << endl; 
	cout << "						 SPHERE					   " << endl << endl;
	cout << "  ----------------------------------------------------" << endl << endl;
	cout << "  r = Radius							  r = " << r << endl;
	cout << "												   " << endl << endl;
	cout << "  ----------------------------------------------------" << endl << endl;
	cout << "  a = Area of circle within sphere		a = " << a << endl;
	cout << "	  a = (PI * pow(r, 2))						 " << endl << endl;
	cout << "  ----------------------------------------------------" << endl << endl;
	cout << "  c = Circumference of circle			 c = " << c << endl;
	cout << "	  c = (2 * PI * r)							 " << endl << endl;
	cout << "  ----------------------------------------------------" << endl << endl;
	cout << "  d = Diameter of circle				  d = " << d << endl;
	cout << "	  d = (r * 2)								  " << endl << endl; 
	cout << "  ----------------------------------------------------" << endl << endl;
	cout << "  v = Volume of sphere					v = " << v << endl;
	cout << "	  v = (((4 / 3) * PI) * pow( r, 3 ))		   " << endl << endl;
	cout << "  ----------------------------------------------------" << endl << endl;
} 

//************************************
//Destructor of sphere class. 
//************************************
CSphere::~CSphere()  
{
	
}

//************************************
// MAIN() FUNCTION.
//************************************
int main(int argc, char* argv[])
{
	int	 choice   =	0;
	bool	exitt	=	false;
	
	CSphere OSun;  //Instantiates object OSun of type class CSphere.

	do
	{
		OSun.m_getRadiusSphere();
		OSun.m_DiameterSphere();
		OSun.m_AreaCircle();
		OSun.m_CircumferenceSphere();
		OSun.m_VolumeSphere();
		//************************************
		system("CLS");
		cout << endl << endl << endl;
		OSun.m_DisplayPublicSphere();
		cout << endl << endl << endl;
		cout << "  ";
		system("PAUSE");
		OSun.~CSphere();
		//************************************
		system("CLS");
		cout << endl << endl << endl;
		cout << "  Type the number 1 and press return ";
		cout << "to run the program again.  " << endl << endl;
		cout << "  Type 0 and press return to exit. ==> ";
		cin >> choice;
		if (choice == 0) EXIT_SUCCESS;
		if (choice == 1)
			exitt = false;
		else
			exitt = true;
	}while(exitt == false);

	return EXIT_SUCCESS;
}



III. OBJECTS

Objects are reusable software components that model items in the real world. You can not call a member function of a class until you create an object of that class.

The syntax for instantiating or declaring a class object is as follows:

Class_Name Object_Name;

An example of the syntax used in the attached program is as follows:

CSphere OSun;

The object you create only contains data. All objects of a class share the class member functions. However, the data contained in class object data members varies.

IV. DATA MEMBERS

Data members are declared inside a class definition but outside the bodies of the class member function definitions.

Each object has its own copy of its data members. However, each object shares the class member functions.

Member variables also known as data members are the class variables. A class also contains functions called member functions or methods which determine what a class can do. The member functions in a class manipulate the member variables. Collectively, the data members and member functions are called the class’s members.

As a general rule you should keep the data members of a class private. The private and public keywords enable a feature known as data hiding.

private:

		double a;  //Area of circle.
		double c;  //Circumference of circle.
		double d;  //Diameter of circle.
		double r;  //Radius of circle.
		double v;  //Volume of sphere.



C++ ensures that programmers cannot access private data members outside the class unintentionally.

To access private data in a class, you must create public functions known as accessor methods.

//************************************
//Computes diameter of circle, equals
//d = (r * 2); 
//************************************
void CSphere::m_DiameterSphere()  
{
	d = (r * 2);
}

//************************************
//Computes area of circle, equals
//a = (PI * pow(Radius, 2)); 
//************************************
void CSphere::m_AreaCircle() 
{
	a = (PI * pow(r, 2));
}

//************************************
//Computes circumference of circle, equals
//c = (2*PI*Radius); 
//************************************
void CSphere::m_CircumferenceSphere()  
{
	c = (2 * PI * r);
}

//************************************
//Computes volume of a sphere, equals
//v = (((4/3) * PI) * pow(Radius, 3)); 
//************************************
void CSphere::m_VolumeSphere()   
{
	v = (((4 / 3) * PI) * pow( r, 3 ));
}



These accessor methods are the member functions that other parts of your program call to get and set private member variables. Accessor functions enable the separation of the details of how the data is stored, from how the data is used.

• V. SCOPE

The scope of a variable specifies where it can be used in a program. You get a compiler error when variables that have the same name do not have different scopes. To accommodate these types of problems C++ has several scopes; for example:

1. Function scope.

Variables defined in a member function definition’s body are known as local variables. A local variable must be declared before it can be used. The scope of the local variable is the function in which it is declared. A local variable can not be used outside of the scope of its declaring function. When a function terminates the values of its local variables are destroyed unless they have been declared as static.

Local variables declared as static retain their values when the function within which they have been declared completes. The local variables declared as static are only known in the function in which they are declared. The next time the function containing the static local variables is called the static local variables retain the values they had when the function was last executed.

Labels, identifiers followed by a colon, have function scope.

2. File scope.

A file is the program source code file.

Global variables, global functions, function prototypes, and function definitions placed outside a function all have file scope.

3. Block scope.

Code with braces {…} is a block.

Identifiers declared inside a block have block scope.

Variables declared in a class member function have block scope.

Function parameters, local variables of the function, have block scope.
When blocks are nested and an identifier in an inner block has the same name as an identifier in an outer block. The identifier with the same name in the outer block is hidden until the inner block completes. During the execution of the inner block, the inner block only has awareness of its own identifier’s value. During execution, the inner block has no awareness of the outer block identifier with the same name’s value. If your program code intends to have the outer block executing simultaneously with the inner block the compiler will give you a logic error. It is good programming practice to avoid using duplicate identifier names in a program.

4. Function prototype scope.

The function prototype is the declaration of the function and tells the compiler the name of the function, the function’s return type, and the function’s parameters type, number, and order.

public:
			   CSphere();
		void   m_getRadiusSphere();			  //Radius of circle.
		void   m_DiameterSphere();			   //Diameter of circle.
		void   m_AreaCircle();				   //Area of circle.
		void   m_CircumferenceSphere();		  //Circumference of circle.
		void   m_VolumeSphere();				 //Volume of sphere.
		void   m_DisplayPublicSphere();		  //Dispalys properties of sphere.		 



The function signature is the part of the function prototype that includes the name of the function and the types of its parameters. The function signature does not include the function return type. Functions in the same scope must have unique signatures.

5. Class scope.

The class scope starts with the keyword class, includes the class definition body block within the braces {…}, the class’s class members, class data members, and class member functions.

Within a class’s scope, class members are accessible by all of that class’s member functions and can be referenced by name.

Outside a class’s scope, public class members are referenced by an objects name, a reference to an object, or a pointer to an object. The dot member selection operator (.) is preceded by an objects name to access the object’s members. The arrow member selection operator (->) is preceded by a pointer to an object to access the object’s members.

6. Namespace scope.

Each namespace defines a scope within which identifiers and variables are placed.

To use a namespace member, either the member’s name must be qualified with the namespace name and the binary scope resolution operator ::, or a using declaration or using directive must appear before the name is used in the program. A using declaration; for example, “using std::endl;” brings one name into the scope where the declaration appears. A using directive; for example, “using namespace std;” brings all the names from the namespace into the scope where the directive appears.

• VI. PARAMETERS AND ARGUMENTS

Parameters and arguments are often used as synonyms; however, they have unique meanings. Parameters refer to the function’s declaration and definition. Arguments refer to the function’s call. A function can specify multiple parameters by separating each parameter from the next with a comma. The number and order of arguments in a function call must match the number and order of the parameters in the parameter list of the called member function’s declaration and definition. In addition, the argument types in the function call must be consistent with the corresponding parameter types in the parameter list of the called member function’s declaration and definition.

Four steps are involved in using a class member function.

1. First declare the class member function.

Member function declarations or prototypes do not require names in the parameter list; only types are required. The compiler ignores names if they are used. However, programmers sometimes include names to help them document the program.

The syntax for declaring a class member function is as follows:

Type function_Name(parameter_type_list);

An example of the syntax used in the attached program is as follows:

void m_getDiameterSphere();

2. Second define the class member function.

The syntax for defining a class member function is as follows:

Type Class_Name::member_Function_Name(parameters);

An example of the syntax used in the attached program is as follows:

void CSphere::m_DiameterSphere();

3. Third, code the class member function call and make sure the type, order, and the number of parameters of the called member function’s declaration and definition; and the type, order, and the number of the function call arguments are in agreement.

4. Fourth call the class member function.

The syntax for calling a class member function is as follows:

Object.member_Function_Name (Arguments);

An example of the syntax used in the attached program is as follows:

OSun.m_DiameterSphere();

To avoid ambiguity, do not use the same names for the arguments passed to a function and the corresponding parameters in the function definition.

• VII. MAIN FUNCTION

The function main() is always called automatically when you execute a program.

The main() function creates objects from classes.

//************************************
// MAIN() FUNCTION.
//************************************
int main(int argc, char* argv[])
{
	int	 choice   =	0;
	bool	exitt	=	false;
	
	CSphere OSun;  //Instantiates object OSun of type class CSphere.

	do
	{
		OSun.m_getRadiusSphere();
		OSun.m_DiameterSphere();
		OSun.m_AreaCircle();
		OSun.m_CircumferenceSphere();
		OSun.m_VolumeSphere();
		//************************************
		system("CLS");
		cout << endl << endl << endl;
		OSun.m_DisplayPublicSphere();
		cout << endl << endl << endl;
		cout << "  ";
		system("PAUSE");
		OSun.~CSphere();
		//************************************
		system("CLS");
		cout << endl << endl << endl;
		cout << "  Type the number 1 and press return ";
		cout << "to run the program again.  " << endl << endl;
		cout << "  Type 0 and press return to exit. ==> ";
		cin >> choice;
		if (choice == 0) EXIT_SUCCESS;
		if (choice == 1)
			exitt = false;
		else
			exitt = true;
	}while(exitt == false);

	return EXIT_SUCCESS;
}



The main() function then calls member functions for the objects to accomplish the work of the program. Member functions may create other objects or call other functions. Within each function call, local objects are destroyed when the function completes. When the main() function completes, the objects the main() function has created are also destroyed.

WHAT YOU HAVE LEARNED IN THIS TUTORIAL

1. You have learned how to instantiate or declare a class object.

2. You have learned how to keep the data members of a class private.

3. You have learned how to create public functions known as accessor methods.

4. You have learned how to successfully implement function prototype scope.

5. You have learned the difference between parameters and arguments.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1