fundamental principles of c++ programming …object oriented programming and c++ by r. rajaram. 4....
TRANSCRIPT
Fundamental Principles of C++ Programming Language
Lecture Code: CIT 202
Bowen University, Iwo Nigeria
Learning Objectives: To introduce students’ to the fundamentals of C++
Programming Language
Targeted Students: 200 Level
Course Title: Computer Programming ||
Brief Introduction to Object Oriented Programming
Literally – an Object as STATE and BEHAVIOUR
STATE: are the Descriptive characteristics of the Object
BEHAVIOUR: What the Object can do or what can be done to an Object
Object-based programming is the style of programming that primarily supports encapsulation and object identity.
Object-oriented programming language incorporates all of object-based programming features along with two
additional features, namely, inheritance and dynamic binding.
Other fundamental OOP Concepts like, Classes, Data abstraction, Encapsulation, Inheritance, Polymorphism,
Dynamic binding, Message passing will be discussed later
Introduction to Programming
• A program is a set of instructions in logical sequence, given to a computer to perform a particular task
• Every program requires a medium of communication – Language
• From Machine Language to Low level language, up the ladder, now object-oriented programming
language
• Different integrated development environment are available to begin the coding experience
• The goal of programming is to solve problem through process automation: Coding
• Among other Object Oriented programming languages like Java, Python, C++ will be discussed in this
presentation
C++ language is a direct descendant of C programming language with additional features such as
type checking, object oriented programming, exception handling etc.
C++ Overview
It was created by Bjarne Stroustrup of Bell Labs, originally known as C with Classes; and was renamed
as C++ in 1983
C++ is a general-purpose object-oriented programming language
It’s application domain includes systems software, client application software, embedded software solutions, high-
performance server among others . It is also useful for entertainment software such as video games, native code
for Android applications
C++ programs are easily maintainable and expandable. When a new feature needs to be implemented, it is very
easy to add to the existing structure of an object.
C++ Programming Components
C++ Standard Libraries
C++ Main Method
C++ Variables
C++ Functions
/* C++ Programming Examples */
#include<iostream>
Using namespace std;
int main()
{
cout<<"Hello, Welcome to C++ tutorial class";
return 0;
}
C++ Keywords
A complete C++ Programming will have the above components duly represented
•It supplies functions that you wouldn't write in C++ itself so they can be optimized when solving a problem
•Provides support for language features (e.g. memory management)
•It as provision to implementation-dependent information (like limits)
•It support non-primitive facilities for effective portability (e.g. containers, sort functions, I/O streams)
•Has conventions for extending the facilities it does provide
Overview of C++ Standard Libraries
E.G are ostream>, <list>, <exception>, <vector>, are examples of standard libraries. ,
• They are the Include Files
C++ Main Method
Every C++ program requires a main method. Basically, this is where code execution starts from
Thus, every main () in C++ should end with a return (0) statement; otherwise a warning an error might occur.
The default return type for all function in C++ is int,
Since main () returns an integer type for main () is explicitly specified as int.
1. int: These type of variables holds integer value e,g 1, 5, 10, 356 etc..
2. char: holds character value like ‘a’, ‘F’, ‘Z’, ‘k’, ‘L’ etc.
3. bool: holds Boolean value True or False.
4. double: double-precision floating point value.
5. float: Single-precision floating point value.
C++ Variables Datatypes
Variables can be categorised based on their data type. The following are the available types of variables in C++.
A variable is a name which is associated with a value that can be changed
Variables are the backbone of any programming language. It is an address in primary memory where the value
of the object is stored while the program is executing.
A variable is merely a way to store some information for later use.
Once declared and defined they may be used many times within the scope in which they were declared.
C++ Variables
data_type variable1_name = value1, variable2_name = value2;
C++ Variables Declaration
E.g. int num1 =20;
C++ Variables Assignment R.H.S = L.H.S i.e. Variable = Value
Variables in C++ MUST be declared FIRST before usage
• Values can be assigned using the assignment (=) operator: x=3;
• Read the value in from an input device such as the keyboard: cin >> y;
Local variables are declared inside the braces of any user
defined function, main function, loops or any control
statements(if, if-else etc) and have their scope limited
inside those braces.
A variable declared outside of any function (including main as
well) is called global variable. Global variables have their
scope throughout the program, they can be accessed
anywhere in the program, in the main, in the user defined
function, anywhere.
Types Variables
Local variable
Global variable
void function1()
{
int a, b; // a and b can be used within braces only
}
int a =4;
int b=5;
public int add(){
return a+b;
}
• Variable names in C++ can range from 1 to 255 characters.
• All variable names must begin with a letter of the alphabet or an underscore(_).
• After the first initial letter, variable names can also contain letters and numbers.
• Variable names are case sensitive.
• No spaces or special characters are allowed.
• You cannot use a C++ keyword (a reserved word) as a variable name.
C++ Variable Naming Convention (Identifiers)
C++ Keywords
Keywords are predefined, reserved words which are associated with specific features.
auto double int struct
break else long switch
case enum register typedef
char extern return catch
continue for signed void
do if static while
default goto sizeof volatile
const float short unsigned
Int: declare a integer variable Else: alternate case for an if statement
Const: declare immutable data or functions that do not change data Catch: handles exceptions
Goto: jump to a different part of the program
A comment may start anywhere in the line, and whatever follows till the end of the line is ignored.
/* This is an example of C++ program
to illustrate how comments can be integrated
*/
// This is an example of // C++ program to illustrate // how comments can be integrated
C++ Comments
Comments are portions of the code ignored by the compiler which allow the user to make simple notes
in the relevant areas of the source code.
Single Line Comments
Multiple Line Comments
The goal of comments in coding to ensure proper documentation
When a program runs, the code is read by the compiler line by line (from top to bottom, and for the
most part left to right). This is known as "code flow.“
When the code is being read from top to bottom, it may encounter a point where it needs to make a
decision. Based on the decision, the program may jump to a different part of the code. It may even
make the compiler re-run a specific piece again, or just skip a bunch of code.
C++ Execution Structures
A block is a group of statements which are separated by semicolons (;) like all C++ statements, but
grouped together in a block enclosed in braces: { }:
#include <iostream>using namespace std;
int main(){
int Num1, Num2, sumOfTwoNumbers;
cout << "Enter two integers: ";cin >> Num1 >> Num2 ;
// sum of two numbers in stored in variable sumOfTwoNumberssumOfTwoNumbers = Num1 + Num2;
// Prints sum cout << Num2 << " +1" << Num2 << " = " << sumOfTwoNumbers;
return 0;}
C++ Example One: Addition of Two Numbers
C++ Example Two: Swap Two Numbers
#include <iostream>using namespace std;
int main(){
int a = 10, b = 50, temp;
cout << "Before swapping." << endl;cout << "a = " << a << ", b = " << b << endl;
temp = a;a = b;b = temp;
cout << "\nAfter swapping." << endl;cout << "a = " << a << ", b = " << b << endl;
return 0;}
C++ Example Three: Calculate the Area of a Circle
#include<iostream>
using namespace std;
int main()
{
float r,area;
cout<< "\nEnter radius of circle : ";
cin>>r;
area = 3.14*r*r;
cout<<"Area of circle : "<<area;
return 0;
}
1.Syntax errors: errors due to the fact that the syntax of the language is not respected.
2.Semantic errors: errors due to an improper use of program statements.
3.Logical errors: errors due to the fact that the specification is not respected.
From the point of view of when errors are detected, we distinguish:
1.Compile time errors: syntax errors and static semantic errors indicated by the compiler.
2.Runtime errors: dynamic semantic errors, and logical errors, that cannot be detected by the compiler
Types of program errors
References
1. Object –Oriented –Programming in C++ by E Balagurusamy. .
2. OO Programming in C++ by Robert Lafore, Galgotia Publications Pvt. Ltd.
3. Object Oriented Programming and C++ By R. Rajaram.
4. Object –Oriented –Programming in C++ by Robert Lafore