The C Language
The C++ Language
To define a class, declare the data that it contains and the code that operates on that data. Data is contained in data members, also called instance variables, defined by the class. Code is contained in member functions. We send messages to an object through a function call that tells the member function to perform its task. The functions manipulate the attributes of the object in a prescribed way.
General form of a simple class:
Class-name becomes
a new type, a user-defined type, that can be used to create objects of
that class. For the most part, private items in a class can be
accessed only by other members of their class. This is one way to
achieve encapsulation. Typically, your program will access
the private members of a class through its public functions. A
well-designed class should define only one logical entity.
Encapsulation is a programming mechanism that binds code together with
the data it manipulates, and that keeps both safe from outside
interference and misuse. The code and data are bound in such a way
that a self-contained black box is created that holds all the
necessary parts. When code and data are linked together this way,
an object is created. Thus, and object is the device that
supports encapsulation.
Example
// vehicle.cpp - program to compute the range in miles of a vehicle
#include <iostream>
using namespace std;
// declare Vehicle class
class Vehicle {
public:
// public data members (attributes)
int passengers; // number of passengers in vehicle
int fuel_capacity; // fuel tank capacity in gallons
int mpg; // miles per gallon
// public member function
int range(); // compute and return range (function prototype)
};
// implement the member function, range
int Vehicle::range() // the :: is the scope resolution operator
{
return mpg * fuel_capacity;
}
int main()
{
Vehicle minivan; // create a Vehicle object
Vehicle sportscar; // create another Vehicle object
int range1, range2;
minivan.passengers = 7;
minivan.fuel_capacity = 16;
minivan.mpg = 21;
sportscar.passengers = 2;
sportscar.fuel_capacity = 14;
sportscar.mpg = 12;
// compute ranges, assume full tank
range1 = minivan.range ();
range2 = sportscar.range ();
cout << "Minivan can hold " << minivan.passengers <<
" with a range of " << range1 << " miles.\n";
cout << "Sportscar can hold " << sportscar.passengers <<
" with a range of " << range2 << " miles.\n";
}
|
Output: |
return statement;
cin or
cout;
Syntax: <type>
func_name (param_types);
Examples
double average(int n1, int n2, int n3);
double average2(int, int, int);void greet();
The following sample program shows how greet()
would be used in a program:
#include <iostream>
using namespace std;
void greet(); // prototype, or declaration
int main()
{
cout << "Are you there?\n";
greet();
cout << "I say, are you there?\n";
greet();
}
// function definition
void greet()
{
cout << "Hello!\n";
}
|
Output:
Are you there?
Hello!
I say, are you there?
Hello!
|
Example  
#include<iostream> using namespace std; int add_it(int a, int b, int c); // function prototype -OR- int add_it(int, int, int); |
Output:
Please enter 3 integers:
The sum of 100, -23, and 458 is 535.
The sum of 100, -23, and 458 is 535.
|
sum = add_it(x, y, z);
// x, y, and z are arguments.
(formal) parameter - a placeholder for an actual argument in a function definition. A function header:
int add_it(int a,
int b, int c) // a, b, and c are parameters
Arguments and parameters must match in number and type. They correspond in order, first to first, second to second, etc.
Example
#include <iostream>
using namespace std;
double average (int n1, int n2, int n3);
const int COUNT = 3;
int main()
{
int a, b, c; // values to average
double mean; // average value of three input integers
cin >> a >> b >> c;
mean = average (a, b, c);
cout << "Average: " << mean << endl;
}
|
Input:
4 6 4 2 7 |
Below is one way to write function average( )
that is invoked above:
double average(int n1, int n2, int n3)
{
double sum, // sum of three input parms
mean; // average of the parms
sum = n1 + n2 + n3; // add, and convert to double
mean = sum / COUNT;
return mean;
} |
Below is an alternate version for average( ).
double average1(int n1, int n2, int n3)
{
return (n1 + n2 + n3) * 1.0 / COUNT;
}
|
A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a function. But, constructors have no explicit return type.
General form of a constructor:
Use a constructor to give initial values to the data members defined by the class, or to perform any other start up procedures required to create a fully formed object.
The complement of the constructor is the destructor. A class's
destructor is called implicitly when an object is destroyed. It performs
termination housekeeping before the system reclaims the object's
memory for possible use to hold new objects. Note that local objects
are created when their block is entered and destroyed when the block
is exited. Global objects are destroyed when the program terminates.
A class may have only one destructor, and the destructor may have no
parameters.
Every class has a destructor. If the programmer does not supply one,
the compiler creates an "empty" destructor that actually performs important
operations in some circumstances.
The destructor has the same name and general format as the constructor,
but the name is preceded by a tilde (~). Like constructors, destructors
do not have explicit return types. The code in the destructors is
automatically executed when an object is destroyed.
A version of vehicle.cpp that uses constructors and destructors
is in vehicle2.cpp. Trace
through the code to see if you know what is printed. Notice below that
since we have two Vehicle objects, the destructor is executed twice.
Output:
      Minivan can hold 7 with a range of 336 miles.
      Sportscar can hold 2 with a range of 168 miles.
      Destructing... Run!
      Destructing... Run!
Read the text, section 3.8, Placing a Class in a Separate File for Reusability. File vehicle3.cpp incorporates a user-defined header file; see vehicle3.cpp. The header file is in Vehicle.h.
Data members and member functions in a class that are declared private are accessible only to member functions of their class. Commonly data members are private and member functions are public. This means that we must use member functions to store, modify, and retrieve values with data members; we cannot directly access them from outside functions, including main. This data hiding is desirable, and we expect you to incorporate it into your code. File vehicle4.cpp illustrates private data members; see vehicle4.cpp. The header file is in Vehicle4.h.