All variables in C++ must be declared prior to their
use. This is necessary
because the compiler must know what type of data a variable
contains before it can properly compile any
statement that uses the variable. In C++ there
are seven basic data types: character, wide character, integer,
floating point, double
floating point, Boolean, and somewhat surprisingly, valueless. The
keywords used to declare
variables of these types are char,
chart,
int,
float,
double,
bool,
and void,
respectively. Common sizes and ranges of each data type are shown
in Table 3-1. Remember, the sizes and ranges used
by your compiler may vary from those
listed here. The most common variance occurs between 16-bit and
32-bit environments. In
general, an integer in a 16-bit environment is 16 bits wide. In a
32-bit environment, an integer is usually 32 bits
wide Variables of type char
are used to hold 8-bit ASCII characters such
as A, B, or C, or any other
8-bit quantity. To specify a character, you must enclose it between
single quotes. The type
wchar_t is
designed to hold characters that are part of large character
sets. As you may know, many human languages, such
as Chinese, define a large number
of characters, more than will fit within the 8 bits provided by the
char type.
Complete Package of C++
Pages
- Home
- Arrays & Structures
- structure member initialization
- String Representation and Handling
- Virtual Functions
- Pointers
- Standard Input Output Stream
- Memory Management operators
- Statements & Loops
- Constructors & Destructors
- Polymorphism
- Encapsulation
- Inheritance
- Functions
- Entering Programs
- Overview
- Conversions
- Operators & Expressions
- Exceptions Handling
- Data Types
- Operator overloading
Oops
Tuesday, 9 July 2013
What Is Object-Oriented Programming?
Since object-oriented programming was fundamental to the
development of C++, it is important to define precisely what
object-oriented programming is. Object-oriented programming has taken
the best ideas of structured programming and has combined them with
several powerful concepts that allow you to organize your programs
more effectively. In general, when programming in an object-oriented
fashion, you decompose a problem into its constituent parts. Each
component becomes a self-contained object that contains its own
instructions and data related to that object. Through this process,
complexity is reduced and you can manage larger programs. All
object-oriented programming languages have three things in common:
encapsulation, polymorphism, and inheritance. Although we will
examine these concepts in detail later in this book, let’s take a
brief look at them now.
Encapsulation
As you probably know, all programs are composed of two
fundamental elements: program statements (code) and data. Code
is that part of a program that performs
actions, and data is
the information affected by those actions. Encapsulation
is a programming mechanism that binds
together code and the data it manipulates, and that keeps both safe
from outside interference and misuse. In an object-oriented language,
code and data may be bound together in such a way that a
self-contained black
box is created. Within the box are all
necessary data and code. When code and data are linked together in
this fashion, an object is created. In other words, an object
is the device that supports encapsulation.
Within an object, the code, data, or both may be private to that
object or public. Private
code or data is known to, and accessible only
by, another part of the object. That is, private code or data may not
be accessed by a piece of the program that exists outside the object.
When code or data is public,
other parts of your program may access it,
even though it is defined within an object. Typically, the public
parts of an object are used to provide a controlled interface to the
private elements of the object.
Polymorphism
Polymorphism (from
the Greek, meaning “many forms”) is the quality that allows one
interface to be used for a general class of
actions. The specific action is determined by
the exact nature of the situation. A simple
example of polymorphism is found in the
steering wheel of an automobile. The steering
wheel (i.e., the interface) is the same no
matter what type of actual steering mechanism
is used. That is, the steering wheel works
the same whether your car has manual
steering, power steering, or rack-and-pinion
steering. Therefore, once you know how to
operate the steering wheel, you can drive
any type of car. The same principle can also
apply to programming. For example, consider
a stack (which is a first-in, last-out list).
You might have a program that requires three
different types of stacks. One stack is used
for integer values, one for floating-point
values, and one for characters.
In this case, the algorithm that implements each stack
is the same, even though the data being
stored differs. In a non-object-oriented language,
you would be required to create three
different sets of stack routines, calling each set by a different
name, with each set having its own interface. However, because of
polymorphism, in C++ you can create one general set of stack routines
(one interface) that works for all three specific situations. This
way, once you now how to use one stack, you can use them all.
More generally, the concept of polymorphism
is often expressed by the phrase “one interface, multiple methods.”
This means that it is possible to design a generic interface to a
group of related activities. Polymorphism helps reduce complexity by
allowing the same interface to be used to specify a general class of
action. It is the compiler’s job to select the specific
action (i.e., method) as it applies to each
situation.
You, the programmer, don’t need to do this selection
manually. You need only remember and utilize the general interface.
The first object-oriented programming languages were interpreters, so
polymorphism was, of course, supported at run time. However, C++ is a
compiled language. Therefore, in C++, both run-time and compile-time
polymorphism are supported.
Inheritance
Inheritance is the
process by which one object can acquire the properties of another
object. The reason this is important is that it supports the concept
of hierarchical classification. If you think about it, most knowledge
is made manageable by hierarchical (i.e., top-down) classifications.
For example, a Red Delicious apple is part of the classification
apple, which
in turn is part of the fruit
class, which is under the larger class food.
That is, the food
class possesses certain qualities (edible,
nutritious, etc.) that also apply, logically, to its fruit
subclass. In addition to these qualities, the
fruit class
has specific characteristics (juicy, sweet, etc.) that distinguish it
from other food. The apple
class defines those qualities specific to an
apple (grows on trees, not tropical, etc.).
A Red Delicious apple would, in turn, inherit all the
qualities of all preceding classes, and would define only those
qualities that make it unique. Without the use of hierarchies, each
object would have to explicitly define all of its characteristics.
However, using inheritance, an object needs to define only those
qualities that make it unique within its class. It can inherit its
general attributes from its parent. Thus, it is the inheritance
mechanism that makes it possible for one object to be a specific
instance of a more general case.
C++ Implements OOP
As you will see as you progress through this book, many
of the features of C++ exist to provide support for encapsulation,
polymorphism, and inheritance. Remember, however, that you can use
C++ to write any type of program, using any type of approach. The
fact that C++ supports object-oriented programming does not mean that
you can only write object-oriented programs. As with its predecessor,
C, one of C++’s strongest advantages is its flexibility.
Overview
Tuesday, 9 July 2013
An Overview of C++
one
no element exists in isolation. Rather, the components of the
language work together. It is this interrelatedness that makes it
difficult to discuss one aspect of C++ without involving another. To
help overcome this problem, this chapter provides a brief overview of
several core C++ features, including the general form of a C++
program, some simple control statements, variables, and operators. It
does not go into too many details, but rather concentrates on the
general concepts common to all C++ programs. Most of the topics
presented here are examined more closely in later chapters. Since
learning is best accomplished by doing, it is recommended that you
work through the examples using your computer.
Your First C++ Program
Before getting into any theory, let’s look at a simple
C++ program. We will start by
Entering, compiling, and running the following program.
/*
Program #1 - A first C++ program.
Enter
this program, then compile and run it.
*/
#include
< iostream>
using
namespace std;
//
main () is where program execution begins.
Int
main()
{
cout
<< "This is my first C++ program.";
return
0;
}
You will follow these steps.
1. Enter the program.
2. Compile the program.
3. Execute the program.
Before beginning, it is necessary to define two terms.
The first is source
code. Source code is the version of your
program that humans can read. The preceding listing is an example of
source code. The executable version of your program is called object
code or executable
code. Object code is created by the compiler
when it compiles your program.
Subscribe to:
Posts (Atom)