You are on page 1of 7

A procedure, also called a function or a method, is a set of specific instructions executed one after the other.

The data was 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 concept 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 selfcontained enough that each is easily understood. As an example, computing the average salary of an 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 the average salary for each employee.

Totaling the salaries can be broken down into the following steps: 1. Get each employees record. 2. Access the salary. 3. Add the salary to the running total. 4. Get the next employees record. In turn, obtaining each employees record can be broken down into the following: 1. Open the file of employees. 2. Go to the correct record. 3. Read the data.

Limitations of structured programming


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. There are three pillars of oop development: encapsulation, inheritance, and polymorphism.

Encapsulation
When an engineer needs to add a resistor to the device she is creating, he doesnt typically build a new one from scratch. She walks over to a bin of resistors, examines the colored bands that indicate the properties, and picks the one she needs. The resistor is a black box as far as the engineer is concernedshe doesnt much care how it does its work, as long as it conforms to her specifications. She doesnt need to look inside the box to use it in her design. The property of being a self-contained unit is called encapsulation. With encapsulation, you can accomplish data hiding. Data hiding is the highly valued characteristic that an object can be used without the user knowing or caring how it works internally. Similarly, when the engineer uses the resistor, she need not know anything about the internal state of the resistor. All the properties of the resistor are encapsulated in the resistor object; they are not spread out through the circuitry. It is not necessary to understand how the resistor works to use it effectively.

Inheritance and Reuse


When the engineers at Acme Motors want to build a new car, they have two choices: They can start from scratch, or they can modify an existing model called Star. Perhaps their Star model is nearly perfect, but they want to add a turbocharger and a six-speed transmission. The chief engineer prefers not to start from the ground up, but rather to say,Lets build another Star, but lets add these additional capabilities. Well call the new model a Quasar. A Quasar is a kind of Star, but a specialized one with new features. With inheritance, you can declare a new type that is an extension of an existing type. This new subclass is said to derive from the existing type and is sometimes called a derived type. If the Quasar is derived from the Star and, thus, inherits all the Stars qualities, the engineers can add to them or modify those qualities as needed.

Polymorphism
A new Quasar might respond differently than a Star does when you press down on the accelerator. The Quasar might engage fuel injection and a turbocharger, whereas the Star simply lets gasoline into its carburetor. A user, however, does not have to know about these differences. He can just floor it, and the right thing happens, depending on which car hes driving. Different objects can be treated similarly and still do the right thing through what is called function polymorphism and class polymorphism. Poly means many, and morph means form. Polymorphism refers to the same name taking many forms,

Before you Program


C++, perhaps more than other languages, demands that the programmer design the program before writing it. Simple problems do not require much design. Complex problems, however, such as the ones professional programmers are challenged with every day, do require design, and the more thorough the design, the more likely it is that the program will solve the problems it is designed to solve, on time and on budget. A good design also makes for a program that is relatively bug-free and easy to maintain. It has been estimated that fully 90% of the cost of software is the combined cost of debugging and maintenance. To the extent that good design can reduce those costs, it can have a significant impact on the bottom-line cost of the project. The first question you need to ask when preparing to design any program is, What is the problem Im trying to solve? Every program should have a clear, well-articulated goal, and youll find that even the simplest programs in this book do so. The second question every good programmer asks is, Can this be accomplished without resorting to writing custom software? Reusing an old program, using pen and paper, or buying software off the shelf is often a better solution to a problem than writing something new. The programmer who can offer these alternatives will never suffer from lack of work; finding less expensive solutions to todays problems always generates new opportunities later.

Assuming that you understand the problem and it requires writing a new program, you are ready to begin your design. Fully understanding the problem (analysis) and creating a plan for a solution (design) form the necessary foundation for writing a world-class commercial application. But the most import thing that you should learn is unlearn what you have learnt in C.

Basics of a C++ program.


A C++ program is a collection of one or more subprograms (functions) Function Collection of statements Statements accomplish a task

Every C++ program has a function called main

It will also consist of the following; Input statements statements which will help accept input data for the program. Processing statements/ expressions/ equations for processing the data as per requirement. Output statements statements which will present the results of the processing. Processing statements/expressions/equations will contain variables. These variables will be used to store data. Processing statements will also consist of operators for operation on the data.

Data types in C++ A major part of understanding how to design and code programs is centered in understanding the types of data that we want to manipulate and how to manipulate that data. When programming, we store the variables in our computer's memory, but the computer has to know what kind of data we want to store in them, since it is not going to occupy the same amount of memory to store a simple number than to store a single letter or a large number, and they are not going to be interpreted the same way. The memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C++. A byte can store a relatively small amount of data: one single character or a small integer (generally an integer between 0 and 255). In addition, the computer can manipulate more complex data types that come from grouping several bytes, such as long numbers or non-integer numbers. In the following table you have a summary of the basic fundamental data types in C++, as well as the range of values that can be represented with each one:

Operators and precedence

The highest precedence operators appear first in the list. Operators in the same box each have equal precedence, and the order of application depends on the associativity labeled in the rightmost column.

Input/Output statements The basic data type for the input/output in c++ is the stream. The most basic stream types are the standard input/output stream. Istream cin basic standard input stream , by default associated with the keyboard. Ostream cout basic standard output stream, by default associated with the monitor. The input/output streams can be accessed through the directive #iostream. Control structures Program control statements are those statement which are used to divert or control the flow of the program during its operation. C++ categories the control staments in the following categories or groups.

1- Selection: consists of if and switch statements. 2- Iteration (Loops): consists of while and for statements. 3- Jump: consists of goto , break, continuo, and return switch statements. 4- Label: consists of case and default statements. 5- Expression 6- Block The selection statements are the if and switch. The term conditional statement is often used in place of selection statement. The iteration statements are while, for, and do/while. These are also commonly called loop statements. The jump statements are break, continue, goto, and return. The label statements include the case and default statements and the label statement itself . Expression statements are statements composed of a valid expression. Block statements are simply blocks of code. (A block begins with a { and ends with a }.) Block statements are also referred to as compound statements.