You are on page 1of 36

Learning Goals

By the end of this courseware, the student is expected to: 1. Understand object-oriented programming and its applications. 2. Recognize the different control structures of C++. 3. Create C++ programs based on certain requirements. 4. Demonstrate the ability to modify and debug C++ programs.

Lesson 1

Programming Languages and Paradigms

L1: Programming Languages and Paradigms

Evolution of Programming Languages


First Generation: Machine Languages use a binary code (strings of 1s and 0s) that can be understood by the computer and executed directly without any need for translation Second Generation: Assembly Languages use mnemonics (very short words) for commands Third Generation: High-Level Languages use data structures and control structures that are abstractions of programming concepts

L1: Programming Languages and Paradigms

Evolution of Programming Languages


Fourth Generation: Declarative Languages also called non-procedural specification languages; a programmer who writes 4GL programs concentrates on what needs to be done (result/output) rather than how to do it (steps/process)

Fifth Generation: AI problem-solving based on constraints or rules that have been declared in the program

L1: Programming Languages and Paradigms

Overview of Programming Paradigms


Imperative Paradigm creates a sequence of commands or instructions for the computer to follow Functional Paradigm computations are specified through mathematical functions that evaluate input expressions and convert them into output values Logic Paradigm viewed as a logical theory and computation is basically the search for proof

L1: Programming Languages and Paradigms

Overview of Programming Paradigms


Object-Oriented Paradigm (OOP) an attempt to model a real-world system; C++ falls under this paradigm Concurrent Paradigm allows for the execution of two or more operations at the same time Distributed Paradigm concurrent programming implemented on a multiprocessor or multi-computer platform

Lesson 2

C++ Overview

L2: C++ Overview


C++ Roots C++ conceptualized by Bjarne Stroustrup; has the elements of classic programming approaches & OOP OOP Concepts Class group of objects having the same properties, operations, and behaviors Object instance of a class Function subprogram that does a specific task

L2: C++ Overview


OOP Concepts (contd.) Data numerical or other information in a form that may be processed by a computer Encapsulation process of combining data and function to form a class or object Inheritance instead of creating a program in order to simulate an existing one, you can simply invoke that program and extend it by adding new functions Polymorphism a function taking many forms

L2: C++ Overview

Getting Started with C++


1. Inside

the BIN folder of your Turbo C++ installation folder, run TC (the one with the MS-DOS icon). 2. On the screen, press Alt+O and choose Directories. In the Directories dialog box, you can configure where the directories of files can be found, such as source files and others.

L2: C++ Overview Getting Started with C++


3.

Go to File > New. Type this:


#include <iostream.h> #include <conio.h> int main() { clrscr(); cout << C++ is cool! \n; getch(); return 0; }

This program displays the sentence C++ is cool! on your screen.

L2: C++ Overview

Getting Started with C++


4. After

typing the lines of code, press F2 to save. For this sample, name the program cool.cpp and then press Alt+F9 to compile. 5. Press Ctrl+F9 to run your program (or click Run > Run).

Lesson 3

Integrated Development Environment (IDE)

L3: IDE

The Turbo C++ IDE

TechFactors, Inc.

L3: IDE
The Turbo C++ IDE
Like the graphical user interface (GUI) in office applications, the integrated development environment (IDE) makes programming easier because instead of having to manually code the commands so that MS-DOS will be able to execute your programs, it offers you

an interface that combines the individual


actions involved in writing, compiling, and running programs.

L3: IDE
The Main Menu

L3: IDE
The Main Menu (contd.)

Lesson 4

C++ Basics

L4: C++ Basics


General Format

TechFactors, Inc.

L4: C++ Basics Parts of a C++ Program


Preprocessor Directives always start with a hash symbol (#); allow the programmer to use different functions effectively In cool.cpp, the line #include <conio.h> is a preprocessor directive. As you can see, # is followed by the command include, which orders the program to use the file conio.h. This line is very important because without it, the command getch() will not execute properly since it was imported from conio.h.

L4: C++ Basics


Parts of a C++ Program (contd.)
Global Declarations contains the userdefined constants and variables Constants fixed values used in certain mathematical or programming contexts Variables symbols used in a program that stands for a value or data stored in a computers memory Local Variables similar to variables found under global declarations but they can only be accessed and manipulated within the code block they were initialized in

L4: C++ Basics

Functions

All C++ programs need at least one function for it to execute properly: the main() function. main() is always called first during program execution. If a program is executed on its own and it does not have a main() function anywhere on it, it will not run at all!

L4: C++ Basics

Functions
In the diagram, f1(), f2(), fn(), and main() are the functions. Even if f1() up to fn() are missing, as long as main() is present, the program will still compile and run assuming that all the codes inside the main() function can stand on their own and are not crossreferencing to other functions.

L4: C++ Basics

Statements

comprise the bulk of codes found in a function instructional codes that tell the computer to do a certain action upon its execution a semicolon (;) signifies the end of any statement the declaration of a variable in a function is also considered as a statement since the entire line of code ends with a semicolon

L4: C++ Basics

Comments

lines of code that are not executed during the program used either to give a brief labeling and explanation to lines of codes or to temporarily disable lines of code during the development phase

L4: C++ Basics

Comments (contd.)
Two kinds of comments that C++ utilizes: Single-line Comment a double slash (//) at the beginning of a line of code disables the entire statement and considers it merely as a comment Multi-line Comment several lines of code can be disabled by placing the code block between a slash-asterisk (/*) and an asterisk-slash (*/)

L4: C++ Basics

Keywords

reserved words in a programming language that have specific uses (e.g., executing a command) examples: if, do-while, and switch statements main() getch(), and clrscr() functions

L4: C++ Basics Data Types


The data type of a variable determines what kind of values it can store within itself.

TechFactors, Inc.

L4: C++ Basics


The cout and cin Statements
Going back to cool.cpp, the code cout << C++ is cool! \n; displays the sentence on your screen because the cout statement outputs all the variables or constants that are declared in it. In contrast, the cin statement asks for an input from the user. Try this C++ program:
#include <iostream.h> #include <conio.h> int main() { int int1; cout << "\n Enter an integer: \n"; cin >> int1; cout << int1; getch(); return 0; }

cin >> int1; asks for

user input then places whatever the input is on the variable int1 (which was declared to be an int data type in the line int int1;)
cout << int1; displays

the value of int1 after the user input

L4: C++ Basics

Constants
Like variables, constants also act like containers which are userdefined and are given initial values. However, the values of constants can never change or be changed.

L4: C++ Basics

Constants (contd.)
Two ways to declare constants:
1.

By declaring a statement under the preprocessor directives and making use of the command #define. This yields a constant declaration, e.g., #define standardAge 18, where standardAge is the constant name and 18 is the declared value. By declaring a statement under the global declarations and making use of the const command. This yields a constant declaration, e.g., const int standardAge = 18;, where int is the data type for the constant, standardAge is the constant name, and 18 is the declared value. Note that the whole statement ends with a semicolon.

2.

L4: C++ Basics

Operators
Operators are symbols that execute a corresponding action during the program. In C++, there are several categories of operators, each having a unique feature. Assignment Operator (=) assigns a value to an operand

L4: C++ Basics

Operators (contd.)
Mathematical Operators (+, -, *, /, %) addition (+), subtraction (-), multiplication (*), division (/), and modulo operator (%) which is like the division operator but instead of giving the quotient, it yields the remainder

L4: C++ Basics

Operators (contd.)
Increment (++) and Decrement Operators ( - - ) increase and decrease the original values assigned to them by 1 example: a++; will yield an equivalent result to a=a+1; a --; is the same as a=a1;

L4: C++ Basics

Operators (contd.)
Relational Operators (==, !=, >, <, >=, <=) perform a comparison test between variables and constants Logical Operators (&&, ||, !) further expand the relational operators since they add more control to it

L4: C++ Basics


Expressions

statements that return a value after being invoked or executed

C++ Operator Precedence


() ! * + < == && || TechFactors, Inc. ++ / <= != >= > % --

You might also like