Introduction to Classes and Objects

Procedural, Structured, and Object-Oriented Programming
A procedure, also called a function or a method, is a set of specific instructions executed one after the other. The data was quite separate from the procedures, and the trick in programming was to keep track of which functions called which other functions, and what data was changed. To make sense of this potentially confusing situation, structured programming was created.
The principal idea behind structured programming is the idea of divide and conquer. A computer program can be thought of as consisting of a set of tasks. Any task that is too complex to be described simply is broken down into a set of smaller component tasks, until the tasks are sufficiently small and self-contained enough that they are each easily understood.
As an example, computing the average salary of every employee of a company is a rather complex task. You can, however, break it down into the following subtasks:
1. Count how many employees you have.
2. Find out what each employee earns.
3. Total all the salaries.
4. Divide the total by the number of employees you have.
Totaling the salaries can be broken down into the following steps:
1. Get each employee’s record.
2. Access the salary.
3. Add the salary to the running total.
4. Get the next employee’s record.
In turn, obtaining each employee’s record can be broken down into the following:
1. Open the file of employees.
2. Go to the correct record.
3. Read the data.
Structured programming remains an enormously successful approach for dealing with complex problems. By the late 1980s, however, some of the deficiencies of structured programming had become all too clear.
First, a natural desire is to think of data (employee records, for example) and what you can do with that data (sort, edit, and so on) as a single idea. Unfortunately, structured programs separate data structures from the functions that manipulate them, and there is no natural way to group data with its associated functions within structured programming. Structured programming is often called procedural programming because of its focus on procedures (rather than on “objects”).
Second, programmers often found themselves needing to reuse functions. But functions that worked with one type of data often could not be used with other types of data, limiting the benefits gained.
Object-Oriented Programming (OOP)
Object-oriented programming responds to these programming requirements, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.
The essence of object-oriented programming is to model “objects” (that is, things or concepts) rather than “data.” The objects you model might be onscreen widgets, such as buttons and list boxes, or they might be real-world objects, such as customers, bicycles, airplanes, cats, and water.
Objects have characteristics, also called properties or attributes, such as age, fast, spacious, black, or wet. They also have capabilities, also called operations or functions, such as purchase, accelerate, fly, purr, or bubble. It is the job of object-oriented programming to represent these objects in the programming language.
Object-oriented programs work the other way around. They are organized around data, with the key principle being "data controlling access to code." In an object-oriented language, you define the data and the routines that are permitted to act on that data. Thus, a data type defines precisely what sort of operations can be applied to that data.
To support the principles of object-oriented programming, all OOP languages have three traits in common: encapsulation, polymorphism, and inheritance. Let's examine each.
Classes
Classes are the most important feature in C++. Early versions of the language were named "C with Classes," emphasizing the central role of the class facility. As the language evolved, support for building classes increased. A primary goal of the language design has been to provide features that allow programmers to define their own types that are as easy and intuitive to use as the built-in types.
C++ supports object-oriented programming with a traditional, statically-typed, class-based object model. That is, a class defines the behavior and the state of objects that are instances of the class. Classes can inherit behavior from ancestor classes. Virtual functions implement type polymorphism, that is, a variable can be declared as a pointer or reference to a base class, and at runtime it can take on the value of any class derived from that base class. C++ also supports C-style structures and unions using the same mechanism as classes.
The C++ programming language allows programmers to separate program-specific data types through the use of classes. Classes define types of data structures and the functions that operate on those data structures. Instances of these data types are known as objects and can contain member variables, constants, member functions, and overloaded operators defined by the programmer. Syntactically, classes are extensions of the C struct, which cannot contain functions or overloaded operators.
A class definition starts with the class, struct, or union keyword. The difference between a class and a struct is the default access level. A union is like a struct for which the storage of all the data members overlap, so you can use only one data member at a time.
A class definition can list any number of base classes, some or all of which might be virtual.
In the class definition are declarations for data members (called instance variables or fields in some other languages), member functions (sometimes called methods), and nested types.
A class definition defines a scope, and the class members are declared in the class scope. The class name itself is added to the class scope, so a class cannot have any members, nested types, or enumerators that have the same name as the class. As with any other declaration, the class name is also added to the scope in which the class is declared.
You can declare a name as a class, struct, or union without providing its full definition. This incomplete class declaration lets you use the class name in pointers and references but not in any context in which the full definition is needed. You need a complete class definition when declaring a nonpointer or nonreference object, when using members of the class, and so on. An incomplete class declaration has a number of uses:
C++ has been certified as a 99.9 percent pure standard. This makes it a portable language. There is a C++ compiler for every major operating system, and they all support the same C++ language.
Objects
Objects are the basic run-time entities in an object-oriented system. They may represent a person, a place, a bank account, a table or any data or any item that the program has to handle. They may also represent user-defined data such as vectors, time and lists. Programming problem is analyzed in terms of objects and the nature of communication between them. Program objects should be chosen such that they match closely with the real-wolds objects. object take up space int the memory and have an associated address like a record in Pascal, or a Structure in C.
A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. Following statements declare two objects of class Box:
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
An object doesn't exist until an instance of the class has been created; the class is just a definition. When the object is physically created, space for that object is allocated in RAM. It is possible to have multiple objects created from one class.
Objects may be passed to functions in just the same way that any other type of variable can. Objects are passed to functions through the use of the standard call-byvalue mechanism. This means that a copy of an object is made when it is passed to a function. However, the fact that a copy is created means, in essence, that another object is created. This raises the question of whether the object's constructor function is executed when the copy is made and whether the destructor function is executed when the copy is destroyed.
Benefits of OOP
OOP offers serveral benefits to both the program designer and the User. The new Technology promises greater progrmmer productivity, better quality of software and lesser maintenance cost. The Principal advantages are.
* In inheritance, We can eliminate redundant code and extend the user of existing classes.
* We can build programs from the standard working modules that communicate with one another, rather than having to start writing the code from scratch. This leads to saving of development time and higher productivity.
* The principle of data hiding helps the prorammer to buil secure programs that cannot be invaded by code in other parts of the program.
* It is possible to have multiple instances of an object to co-exist without any interference.
* It is possible to map objects in the problem domain to those in the program.
* The data-centered design approach enables us to cpature more details of a model in implementable form.
* Object-oriented Systems can be easily upgraded from small to large Systems.
*Message Passing techniques for communication between objects makes the interface descriptions with external system much simpler.
*Software complexity can be easily managed.
Developing a software that is easy to use makes it hard to build. It is hoped that the object-oriented programming tools would help manage this problem.
Implicit Conversion
We can mix data types in expressions
m=5+2.75
Data types are mixed in an expression, C++ performs the conversion automatically. This process is known as implicit or automatic conversion.
When the compiler encounters an expression, it divides the expressions into sub exressions consisting of one operator and one or two operands. For a binary operator, if the operands type differ, teh compiler converts one of them to match with the other, using the rule the "smaller" type is converted to the "wider" type. For example, if one of the operand is an int and the other is a float, the int is converted into a float because a float is wider than an int.

Whenever a char or short int appears in an expression,it is converted to an int. This is called integral widening conversion. The implicit conversion is applied only after completing all integral widening conversions.
Results of Mixed Mode Operations
Layer 1 RHO LHO
char
short
int
long
float
double long double
char
int int int long float double long double
short
int int int long float double long double
int
int int int long float double long double
long
long long long long float double long double
float
float float float float float double long double
double
double double double double double double long double
long double long double long double long double long double long double long double long double


Free Web Hosting