Defining a Class with a Member Function using C++

First we describe how to define a class and a member function. Then we explain how an object is created and how to call a member function of an object. The first few examples contain function main and the GradeBook class it uses in the same file.

/Define class GradeBook with a member function displayMessage;
Create a GradeBook object and call its displayMessage function.*/
#include <iostream>
using std::cout;
using std::endl;

// GradeBook class definition
class GradeBook
{
public:
// function that displays a welcome message to the GradeBook user
void displayMessage()
{
cout << "Welcome to the Grade Book!" << endl;
} // end function displayMessage
}; // end class GradeBook
// function main begins program execution
int main()
{
GradeBook myGradeBook;
// create a GradeBook object named myGradeBook
myGradeBook.displayMessage();
// call object's displayMessage function
return 0; // indicate successful termination
} // end main

Class GradeBook:
Before function main can create an object of class GradeBook, we must tell the compiler what member functions and data members belong to the class. This is known as defining a class. The GradeBook class definition a member function called displayMessage displays a message on the screen . The class definition begins with the keyword class followed by the class name GradeBook. By convention, the name of a user-defined class begins with a capital letter, and for readability, each subsequent word in the class name begins with a capital letter. This capitalization style is often referred to as camel case, because the pattern of uppercase and lowercase letters resembles the silhouette of a camel. Every class's body is enclosed in a pair of left and right braces ({ and }).

The access-specifier label public:. The keyword public is called an access specifier. The member function appears after access specifier public: to indicate that the function is "available to the public"that is, it can be called by other functions in the program and by member functions of other classes. Access specifiers are always followed by a colon (:). For the remainder of the text, when we refer to the access specifier public, we will omit the colon. There's a second access specifier private.

Each function in a program performs a task and may return a value when it completes its taskfor example, a function might perform a calculation, then return the result of that calculation. When you define a function, you must specify a return type to indicate the type of the value returned by the function when it completes its task. In the code, keyword void to the left of the function name displayMessage is the function's return type. Return type void indicates that displayMessage will perform a task but will not return (i.e., give back) any data to its calling function when it completes its task.

The name of the member function, displayMessage, follows the return type. By convention, function names begin with a lowercase first letter and all subsequent words in the name begin with a capital letter. The parentheses after the member function name indicate that this is a function. An empty set of parentheses, indicates that this member function does not require additional data to perform its task.

The body of a function contains statements that perform the function's task. In this case, member function displayMessage contains one statement that displays the message "Welcome to the Grade Book!". After this statement executes, the function has completed its task.

Note:
  1. Forgetting the semicolon at the end of a class definition is a syntax error.
  2. Returning a value from a function whose return type has been declared void is a compilation error.
  3. Defining a function inside another function is a syntax error.

0 comments:

Post a Comment

 

About Me

It's Me!Hi, I'm Moinuddin. I'm a Software Engineer at WIPRO working on Microsoft .NET Technology. I'm interested in a wide range of technology topics, mainly Microsoft including Windows Azure, WCF, WPF, Silverlight and any other cool technology that catches the eye.

Site Info

ProjectCSharp.com is mainly about demonstrating real time examples in different technologies like ASP.NET, WCF, WPF, Silverlight, Azure etc..,

Followers

Help Links

Project CSharp (C#) Copyright © 2011. All Rights Reserved.