Top Banner
C++ C++ Introduction and Overview
24

C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

Jan 17, 2016

Download

Documents

Toby Singleton
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

C++C++Introduction and Overview

Page 2: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

Introduction

It can be seen from this picture that although this depicts a central figure, it consists of separate images which if arranged or assembled in a certain manner would form the portrait of Don Quixote de la Mancha Miguel de Cervantes’s hero of his epic novel.

The concept of C++ programming can be likened to the painting with the use of objects in making the program structure.

Forewarned, forearmed; to be prepared is half the victory.

- Miguel de Cervantes

Page 3: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

Training Objectives

• Learn about the C++ history.• Understand the programming concepts that are

implemented in the C++ language namely, Object Oriented Programming, Encapsulation, Inheritance, Polymorphism and others.

• Get acquainted with the C++ Integrated Development Environment (IDE) and learn how to navigate through the menus and basic functions.

• Learn to write, compile and run a basic C++ program.

Page 4: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

What’s In It for Me

After completing this course, you will understand how C++ works, know how to make a basic C++ program and run and compile it in the Integrated Development Environment (IDE). This will allow you to learn the fundamental skills to create C++ programs in the future and use similar programming techniques that are applied to other programming languages.

Page 5: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

History of C++

C++ originated from C which was Developed by AT&T in the late 1980s.

C was the most popular programming language for commercial software.

For use in large-scale software development, C was modified to provide features and functions that facilitate object-oriented programming (OOP).

Page 6: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

C & C++

Page 7: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

C & C++

C• Procedural Approach –

program is performed step-by-step in a series

• Structured Approach – program with a complicated engine is divided into several small subsets that are each performed separately, later building up to be the whole set.

C++• Applies/retains the key

features of C and added Object Oriented Programming (OOP).

Page 8: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

Object Oriented ProgrammingObject Oriented Programming

• Object-oriented programming (OOP) is a programming paradigm that uses "objects" and their interactions to design applications and computer programs. It is based on several techniques, including inheritance, modularity, polymorphism, and encapsulation. Many modern programming languages now support OOP.

Page 9: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

OOP is…

– a collection of cooperating objects, as opposed to the traditional view -- list of instructions to the computer.

– each object is capable of receiving messages, processing data, and sending messages to other objects.

– Gives strong emphasis on modularity, object oriented code is intended to be simpler to develop and easier to understand later on, lending itself to more direct analysis, coding, and understanding of complex situations and procedures.

Page 10: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

OOP Techniques

• Encapsulation 

• Inheritance

• Polymorphism

Page 11: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

Encapsulation

• Encapsulation conceals the functional details of a class from objects that send messages to it.

• For example, the Dog class has a bark() method. The code for the bark() method defines exactly how a bark happens (e.g., by inhale() and then exhale(), at a particular pitch and volume).

Timmy, Lassie's friend, however, does not need to know exactly how she barks. Encapsulation is achieved by specifying which classes may use the members of an object. The result is that each object exposes to any class a certain interface — those members accessible to that class.

Page 12: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

Rationale:

• The reason for encapsulation is to prevent clients of an interface from depending on those parts of the implementation that are likely to change in future, thereby allowing those changes to be made more easily, that is, without changes to clients. – For example, an interface can ensure that

puppies can only be added to an object of the class Dog by code in that class.

Page 13: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

Inheritance 

"Subclasses" are more specialized versions of a class, which inherit attributes and behaviors from their parent classes, and can introduce their own.

• For example, the class Dog might have sub-classes called Collie, Chihuahua, and GoldenRetriever. In this case, Lassie would be an instance of the Collie subclass. Suppose the Dog class defines a method called bark() and a property called furColor. Each of its sub-classes (Collie, Chihuahua, and GoldenRetriever) will inherit these members, meaning that the programmer only needs to write the code for them once.

Page 14: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

Example

Each subclass can alter its inherited traits.

• Collie class might specify that the default furColor for a collie is brown-and-white.

• The Chihuahua subclass might specify that the bark() method produces a high-pitched by default.

• Subclasses can also add new members. The Chihuahua subclass could add a method called tremble(). So an individual Chihuahua instance would use a high-pitched bark() from the Chihuahua subclass, which in turn inherited the usual bark() from Dog.

Page 15: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

Polymorphism

• “Poly” means many and “morph” form.• is the ability of objects belonging to different types to

respond to method calls of the same name, each one according to an appropriate type-specific behavior.

• In practical terms, polymorphism means that if class B inherits from class A, it doesn’t have to inherit everything about class A; it can do some of the things that class A does differently. This means that the same “verb” can result in different actions as appropriate for a specific class, so controlling code can issue the same command to a series of objects and get appropriately different results from each one.

Page 16: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

Overriding and Overloading

• If a Dog is commanded to speak(), this may elicit a Bark. However, if a Pig is commanded to speak(), this may elicit an Oink. They both inherit speak() from Animal, but their derived class methods override the methods of the parent class; this is Overriding Polymorphism.

• Overloading Polymorphism is the use of one method signature, or one operator such as "+", to perform several different functions depending on the implementation. The "+" operator, for example, may be used to perform integer addition, float addition, list concatenation, or string concatenation. Any two subclasses of Number, such as Integer and Double, are expected to add together properly in an OOP language. The language must therefore overload the concatenation operator, "+", to work this way. This helps improve code readability

Page 17: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

Getting Started with C++

Note:

Since C++ was derived from the original C language, most programs created in C can still be compiled and run on C++. However, slight changes in the syntaxes of keywords may prevent some C programs from being executed.

Page 18: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

Turbo C++ IDE

Page 19: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

C++ Code

#include <iostream.h>#include<conio.h>

int main(){

clrscr();cout << “C++ is cool! \n”;getch();return 0;

}

Page 20: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

Compiling and Running

• After typing the lines of code, press F2 to save (or click File> Save). For this sample, name the program “cool.cpp” and then press Alt+F9 to compile (or click Compile > Compile) to check for errors.

• Press Ctrl+F9 to run your program (or click Run > Run). If all goes well, your screen should look a little something like this.

Page 21: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

Output

C++ is cool!

Page 22: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

C++C++Integrated Development Environment

Page 23: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

Integrated Development Environment (IDE)

Page 24: C++ Introduction and Overview. Introduction It can be seen from this picture that although this depicts a central figure, it consists of separate images.

Menu Item Shortcut Description

File Alt+F Contains generally used commands like creating new files and opening and saving files

Edit Alt+E Contains commands that can change your program content like moving several lines of code to another area or undoing an action.

Search Alt+S Contains commands to run your program.

Run Alt+R Contains commands to compile your program.

Compile Alt+C Contains commands to compile your program.

Debug Alt+D Contains commands that enable your to debug your program step-by-step in order to trace errors.

Project Alt+P Menu contains commands that enable you to manage multiple projects.

Options Alt+O Contains submenus that allow you to change the configuration of file directories, compilers, linkers, and coding environments

Window Alt+W Contains commands that give control of the current window

Help Alt+H Contains an index of help topics on keywords and functions used in C++