News: Phentermine Online With Insurance No Prescription Cheapest Buy Phentermine Buy Phentermine Sat Delivery Cod Phentermine Without A Prescription Saturday Delivery Cheap Phentermine 37 5mg Phentermine Rxdrug Phentermine Order Cod Phentermine 37.5 Mg Tab Phentermine Prices Pharmacy Online Real Phentermine 37.5 Without Prescription Phentermine Overnight Phentermine Didrex Vs Phentermine Buy Phentermine Online No Scrip Cheap Phentermine Extra Cheap Phentermine Danger Phentermine Online Pharmacy Best Price Phentermine Vs Adipex Phentermine Ingestion By Pets

Introduction
Welcome to About.com’s free tutorial on C++ programming. This lesson is an introduction to class templates. Class templates are an advanced topic that we will study in more detail in the lessons in Topics in C++. Class templates provide a way to parameterize the types within a class. This means that rather than explicitly specifying the data type associated with a class member or method, a placeholder is used. When we instantiate an instance of the class, the actual data type is substituted for the parameter, or placeholder. The process of forming a class with specific data types from a class template is called template instantiation.

In addition to parameterizing types, it is possible to use non-type parameters in a class template. These non-type parameters will serve as constants within a particular instance of the class.

Although this may seem complex right now, the basic use of class templates is easy once you see a few examples. In fact, you may be using class templates already in your programs. Many of the container classes, such as vector, in the standard C++ library are implemented as class templates.

The much celebrated CrudeStack class from the last lesson will be used in our examples. For reference, it is reproduced here with the int data type being stored. Typically, when building class templates, it is easier to first write and debug a concrete class. This class is then turned into a template. Here is the concrete “int” version of CrudeStack. I have dropped the “Crude” part of the class name, removed exception handling and slightly modified the push and pop methods in order to simplify the presentation.

class Stack {
public:
Stack() : index(-1) {}
~Stack() {}
void push(int val)
{
// Increment index, then store
data[++index] = val;
}
int pop()
{
// Retrieve, then decrement index
return data[index–];
}

private:
int data[100];
int index;
};

Introduction
Welcome to About.com’s free tutorial on C++ programming. This lesson covers exceptions. Up to this point in the tutorial, we haven’t worried much about errors or exceptions. First, let’s distinguish between errors and exceptions. An error typically refers to a problem that exists in a program when it is written and compiled. It could be a logic error that will result in incorrect results. In this case, the code is correct but the algorithm has an error. This type of error will only be found during program testing or during design reviews of the program. Another type of error is a syntax error. Typically, the compiler finds errors of this type and they are corrected during the coding of the program. Exceptions are errors or anomalies that occur during the execution of a program. They can be the result of unavailability of system resources, such as memory, file space, channels or from data dependent conditions such as a divide by zero, or numerical overflow. Exceptions tend to be rare events but are predicable.

Given that exceptions are somewhat predicable, how should our programs handle them? Broadly, there are three types of responses we can take.

* Not handle the exception. Allow the program to die or core dump.
* Issue a warning. Print out some type of error message, probably at the spot in the code where the exception occurred and then exit.
* Handle the exception gracefully and continue executing.

Certainly, the first way, doing nothing is not acceptable if you want to remain employed or pass your courses. The second way is a little better. Information about the exception is written out, but this is still not ideal. Most real world programs need to be more robust than this. Exceptions need to be handled and corrected. Execution must continue. Your mission to reach Mars can’t fail due to a divide by zero.

Since we must handle exceptions, what features would assist us? Should the same section of code be raising the exception and handling the exception? Suppose an exception occurs in allocating memory. Should the function or method that attempted the allocation be the one to handle it? Can it? Probably some other, higher level, section of code will have the information necessary to decide how to handle the exception. Maybe different programs using the same classes and methods will handle exceptions differently. This points to a separation of the creation of an exception and its handling. The method in which an exception occurs could just alert its caller. This allows code that raises exceptions to be developed separately from code that handles them. If we pass exceptions up to calling routines, it is necessary to have a way to bundle information and for the exception to have some methods to assist in its handling.

The C++ exception mechanism handles both these features.

* Exceptions may be raised and handled in different sections of code.
* Any object, including class objects may be passes back to the handler of an exception. These objects can contain data and methods to assist in handling the exception.

Introduction
Welcome to About.com’s free tutorial on C++ programming. This lesson covers abstract data types and pure virtual functions. An abstract data type is a class that is meant to serve only as an interface for derived classes; it cannot be instantiated. They serve as base classes to other classes that will be instantiated into objects. In earlier lessons, we saw the role of virtual functions in allowing polymorphism in our code. In C++, a pure virtual function is not only intended to be overridden in base classes, it must be. Pure virtual functions provide a way to force derived classes to implement a particular interface.

Let’s return to our Vehicle and Car classes of the last few lessons. Conceptually, the Vehicle class should be an abstract data type. It’s possible to drive a Car, accelerate a Car, decelerate a Car, but not a Vehicle. A vehicle is an abstraction, or concept. A Car can be a concrete object that we can use to achieve some end in either our software world or the physical world. If a vehicle can’t be concrete, if it can’t be instantiated, what’s its purpose? It serves as a model of the abilities and properties a Car will have. A vehicle can provide implementations for some of these abilities through its non-virtual and non-pure virtual methods. It can also provide only an interface for other methods through the use of pure virtual functions. The interface of a method refers to the number and type of arguments and to the return type; it is what the outside world, other classes need to know to use the function. It describes what they must provide and what to expect back.

Introduction
Welcome to About.com’s free tutorial on C++ programming. This lesson covers some ambiguities that can arise with multiple inheritance, and their solutions, including virtual inheritance. We will begin this lesson where we left off in the lesson on multiple inheritance, designing the JetCar class. As you may recall, we were designing a JetCar class to support development of a prototype of my new line of JetCars. We decided that the JetCar, having properties of both Cars and Jets needed to inherit from both classes. Here are the classes for your reference.

class Vehicle {
public:
Vehicle() {cout << “Vehicle Constructor” << endl;}
virtual ~Vehicle() {cout << “Vehicle Destructor” << endl;}

virtual void accelerate() const {cout << “Vehicle Accelerating” << endl;}

void setAcceleration(double a) {acceleration = a;}
double getAcceleration() const {return acceleration;}

protected:
double acceleration;
};

class Car: public Vehicle {
public:
Car() {cout << “Car Constructor” << endl;}
virtual ~Car() {cout << “Car Destructor” << endl;}

virtual void accelerate() const {cout << “Car Accelerating” << endl;}
virtual void drive() const {cout << “Car Driving” << endl;}

private:
// Car inherits acceleration accessors, member
};

class Jet: public Vehicle {
public:
Jet() {cout << “Jet Constructor” << endl;}
virtual ~Jet() {cout << “Jet Destructor” << endl;}

virtual void fly() const {cout << “Jet flying” << endl;}
};

class JetCar: public Car, public Jet {
public:
JetCar() {cout << “JetCar Constructor” << endl;}
virtual ~JetCar() {cout << “JetCar Destructor” << endl;}

virtual void drive() const {cout << “JetCar driving” << endl;}
virtual void fly() const {cout << “JetCar flying” << endl;}
};

Introduction
Welcome to About.com’s free tutorial on C++ programming. This lesson covers multiple inheritance. In C++, a class may have multiple base classes. That is, it may inherit the members and methods of multiple base classes. As an example of this, I will walk you through some recent design I have done for my top secret Jet-Car project. Over the last year, tired of both endless construction on the roads and increased time to pass through tightened airport security, I have embarked on producing the only reasonable solution to my commuting nightmares, a homemade Jet-Car.

As I started on my Jet-Car design, I began with two existing classes I developed for earlier lessons, the Vehicle and Car classes.

#include
using namespace std;

class Vehicle {
public:
Vehicle() {cout << “Vehicle Constructor” << endl;}
virtual ~Vehicle() {cout << “Vehicle Destructor” << endl;}

virtual void accelerate() const {cout << “Vehicle Accelerating” << endl;}

void setAcceleration(double a) {acceleration = a;}
double getAcceleration() const {return acceleration;}

private:
double acceleration;
};

class Car: public Vehicle {
public:
Car() {cout << “Car Constructor” << endl;}
virtual ~Car() {cout << “Car Destructor” << endl;}

virtual void accelerate() const {cout << “Car Accelerating” << endl;}
void drive() const {cout << “Car Driving” << endl;}

private:
// Car inherits acceleration accessors, member
};

int main() {

Car myCar;

myCar.setAcceleration(9.81);
//One “G”

cout << “Accelerating at ” << myCar.getAcceleration() << ” m/(s*s)”;
cout << endl;

myCar.accelerate();
myCar.drive();

}

Introduction
Welcome to About.com’s free tutorial on C++ programming. This lesson covers polymorphism and virtual methods. Polymorphism in C++ is exhibited by the ability of a pointer or reference to a base class type to behave in different ways when it is used to manipulate objects of different subtypes of that base class. Said another way, in our code a base class pointer or reference is used to access an object of some subclass, possibly unknown at compile time and known only during execution time when the program is running. This base class pointer can access the correct subclass method. That is, it can access the method of the object of the subclass it is pointing to or referencing, rather than the corresponding method within the base class.

Confused. Sorry, polymorphism is difficult to explain, but easy to demonstrate. In the last lesson, we saw how to override base class methods and how objects of subtypes called their method rather than the base class method. This worked fine in the examples only because every object was known completely at compile time. The compiler chooses the correct methods to call. This was possible only because no pointers or references of the base class were used to access the subclasses. Why would anyone use a reference or pointer of base class type to access an object of a subclass? Suppose we need to construct a function that will work with all subclasses of a base class. With polymorphism, the function could be coded to expect a pointer or reference to the base class type and behave correctly and distinctly when different subclasses are passed in. As a second example, suppose we wish to make a list or array containing references or pointers to objects of different subtypes of a single base class. If the type of the array is selected to be either reference or pointer of the base class, subclasses of multiple types can be stored. And more importantly, the pointers or references will behave correctly and distinctly according to what subclass they access. This is polymorphism. So, in C++, the first requirement for polymorphism is that the objects must be accessed via pointer or reference.

A second requirement for polymorphism to work is that the methods whose polymorphic behavior is desired must be declared as virtual. Overridden methods not declared as virtual will behave correctly only for statically bound objects; those known at compile time. For base class pointers or references bound to objects during execution, polymorphism will not be seen if a method is not declared virtual. That means that the base class method will be used rather than the subclass method even if the subclass overrides the method. This is again for a base class pointer or reference used on an object of a subclass. A pointer or reference of the type of the subclass would of course call the subclass method, but that wouldn’t be of use in the examples of the previous paragraph. As we will see, it is also possible to dynamically cast a base class pointer into a subclass pointer to get the desired behavior, but this is less useful since it requires knowledge of the subclass of the object that the base class pointer refers.

Introduction
Welcome to About.com’s free tutorial on C++ programming. This lesson covers basic inheritance. Classes in object-oriented programming attempt to model real world entities. Just as an entity has properties and abilities, a class has data members and methods. Both entities in the real world, and classes in the worlds our programs create have various relationships. These relationships help to simplify the task of understanding the systems we create and the entities of which they are constructed.

One such relationship is inheritance. In C++, inheritance defines an “is a” relationship. The derived class is a type of its base class. For instance, a “cat” class could be derived from an “animal” class. A cat is an animal. A cat is a type of animal. A derived class inherits both the data members and methods of its base class. It may also define additional members and methods that support specialized functionality. All of this is best understood by studying some simple examples.

Welcome to About.com’s tutorial on C++ programming. This lesson covers the topics of variable scope and lifetime. Every object in a program has an identifier or name. Many complex questions arise from this seemingly simple statement. Does each identifier need to be unique? Where is an identifier valid? Where can it be used? Where can it not? The answers to these questions are found by understanding the concept of scope.

A variable’s scope is the region of code in which the compiler can uniquely resolve its identifier. Within a particular scope, an identifier must be unique. In C++, objects can have local, global (namespace) or class scope. A variable with local scope is visible only within the function or code block in which it is defined. Variables with global scope are visible and can be used through out a program. Variables have global scope (or namespace scope) if they are defined in the part of the code that is outside of any class or function definition. A class definition also delimits a block of code and a scope.

Closely related to scope is the topic of lifespan or lifetime. How long does a variable exist? Will it exists for the entire duration of a program or only for the duration of a function or method call? As we will see, an object’s lifetime depends largely on its scope.

Local Scope
Functions or class methods define a local scope. Code blocks delimited by brackets, {}, and certain statements also define local scopes. Variables within a particular local scope can only be accessed by code within that particular code block. Outside of the block, they do not exist. Each identifier must be unique within a scope. The compiler must resolve each identifier within a scope to a particular memory location. The definition of an object allocates storage space for the object in memory. During compilation, each name (identifier) must be resolved or linked to a particular physical location in memory. This means that every definition in a local scope must use a distinct identifier.

You have learned the language, studied its syntax, perhaps written small programs as assignments from classes, web tutorials or books and are now ready to tackle a bigger project. What is the first step? Before any programs are written, really before a single character is typed in a file, some design work must be done. For an object-oriented language, such as C++, design starts with choosing classes and defining their relationships.

The three main types of relationships between classes are generalization (inheritance), aggregation, and association.

* Generalization - This implies an “is a” relationship. One class is derived from another, the base class. Generalization is implemented as inheritance in C++. The derived class has more specialization. It may either override the methods of the base, or add new methods. Examples are a poodle class derived from a dog class, or a paperback class derived from a book class.
* Aggregation - This implies a “has a” relationship. One class is constructed from other classes, that is, it contains objects of any component classes. For example, a car class would contain objects such as tires, doors, engine, and seats.
* Association - Two or more classes interact in some manner. They may extract information from each other, or update each other in some way. As an example, a car class may need to interact with a road class, or if you live near any metropolitan area, the car class may need to pay a toll collector class.

Assume that you’ve been asked by the local zoo to write a program that will be used to study animals in order to create better habitats. What classes might be needed? An animal class is a good starting point.

class Animal {
private:
int itsAge;
float itsWeight;
public:
// Accessor methods have been left out as a simplification
void move() {cout << “Animal Moving\n”;}
void speak() {cout << “Animal Speaking\n”;}
void eat() {cout << “Animal Eating\n”;}
}

To better study a particular type of animal, it is necessary to generalize.
class Duck: public Animal {
private:
public:
// Accessor methods have been left out as a simplification
void move() {cout << “Waddle\n”;}
void speak() {cout << “Quack\n”;}
}

The Duck class has inherited some methods and members from Animal, and has made some of its methods more specialized. Not everyone can waddle and quack.

An animal consists of certain parts: for instance, head, body, and skin. It is an aggregation of these parts. The animal class, likewise, can be an aggregation of many other classes.

class Animal {
private:
int itsAge;
float itsWeight;
Head itsHead;
Body itsBod;
Heart itsHeart;
public:
// Accessor methods have been left out as a simplification
void move() {cout << “Animal Moving\n”;}
void speak() {cout << “Animal Speaking\n”;}
void eat() {cout << “Animal Eating\n”;}
}

Association is a logical relationship. The classes need to know each others interfaces and need to interact but there is no formal “is a” or “has a” relationship as in generalization and aggregation, respectively. As an example, a zookeeper class would need associations with the animal classes. Interactions between these classes would be seen through out the zoo program.

Welcome to About.com’s tutorial on C++ programming. This is the second part of a lesson on operator overloading. It presents some general rules, operators in global and namespace scope and the concept of friend functions. Overloaded operators can be declared with within a class or outside in global or namespace scope. In C++ a function declared as a friend to a class can access and manipulate the non-public members of that class. Classes can also be declared as friends of a class as will be described in a later lesson.

General Rules for Operator Overloading

1. Only existing operator symbols may be overloaded. New symbols, such as ** for exponentiation, cannot be defined.
2. The operators ::, .*, . and ?: cannot be overloaded.
3. Operators =, [], () and -> can only be defined as members of a class and not as global functions.
4. At least one operand for any overload must be a class or enumeration type. It is not possible to overload operators involving only built-in data types. For example, an attempt to overload addition, +, for the int data type would result in a compiler error. int operator+(int i, int j) is not allowed.
5. The arity or number of operands for an operator may not be changed. For example, addition, +, may not be defined to take other than two arguments regardless of data type.
6. The precedence of operators is not changed be overloading.

Overloaded Operators in Global and Namespace Scope
In addition to being defined in class scope (within a class), overloaded operators may be defined in global or namespace scope. Global scope means that the operator is defined outside of any function (including main) or class. Namespace scope means that the operator is defined outside of any class but within a namespace, possible within the main program.

Let’s return to the Fraction class introduced in the last lesson. This time, we will enhance the class to allow mathematical operations between Fractions and integers. As a start, on the next page is the Fraction class with an added overloaded operator to handle the addition of an integer to a Fraction.

Next Page »