Introduction to C++
-
Upload
edan-fuller -
Category
Documents
-
view
29 -
download
2
description
Transcript of Introduction to C++
Learners Support Publications www.lsp4you.com
Introduction to Introduction to C++C++
Learners Support Publications www.lsp4you.com
Objectives of this sessionObjectives of this session
What is C++What is C++ A Simple ProgramA Simple Program Comments in C++Comments in C++ Output OperatorOutput Operator Input OperatorInput Operator Cascading of I/O OperatorCascading of I/O Operator Structure of a C++ ProgramStructure of a C++ Program
Learners Support Publications www.lsp4you.com
C++C++
C++ is an object-oriented programming C++ is an object-oriented programming language.language.
Developed by Bjarne Stroustrup at Developed by Bjarne Stroustrup at AT&T Bell Laboratories, USA in the AT&T Bell Laboratories, USA in the early 1980’s.early 1980’s.
Simule 67 + C language Simule 67 + C language C++ C++ 1997 November ANSI/ISO standards 1997 November ANSI/ISO standards
committee standardised C++.committee standardised C++. For developing editors, compilers, For developing editors, compilers,
databases, communication systems, etc.databases, communication systems, etc.
Learners Support Publications www.lsp4you.com
A Simple ProgramA Simple Program
C++ works by giving (separate) instructions C++ works by giving (separate) instructions to the computer. to the computer.
These instructions can be written as functions.These instructions can be written as functions. The primary function used in C++ is called The primary function used in C++ is called
mainmain. This means that every C++ program . This means that every C++ program should have the should have the main()main() function. Because a function. Because a function is an assignment, in order to perform function is an assignment, in order to perform its job, a function has a body. The body of a its job, a function has a body. The body of a function starts with an opening curly bracket function starts with an opening curly bracket "{" and closes with a closing curly bracket "}"."{" and closes with a closing curly bracket "}".
Learners Support Publications www.lsp4you.com
A Simple ProgramA Simple Program
// my first program in // my first program in C++C++
#include <iostream.h>#include <iostream.h>
int main ( )int main ( )
{ {
cout << "Hello World!"; cout << "Hello World!";
return 0;return 0;
}}
Hello World! Hello World!
continue…
Learners Support Publications www.lsp4you.com
Comments in C++Comments in C++
The most basic documentation you have to The most basic documentation you have to perform is to put comments as much as you perform is to put comments as much as you can. Comments help you and other people can. Comments help you and other people who read your code to figure out what you who read your code to figure out what you were doing.were doing.
Comments are not read by the compiler Comments are not read by the compiler while executing your program. while executing your program.
C++ supports two ways to insert comments: C++ supports two ways to insert comments: o // line comment// line commento /* block comment */ /* block comment */
Learners Support Publications www.lsp4you.com
Comments in C++Comments in C++
// The hello.cpp program// The hello.cpp program// Include the iostream library// Include the iostream library#include <iostream.h>#include <iostream.h>int main( )int main( ){{/* Here is a simple sentence that I want to display /* Here is a simple sentence that I want to display when the program starts. It doesn't do much. I am when the program starts. It doesn't do much. I am planning to do more stuff in the future. */planning to do more stuff in the future. */cout << "Hi, this is my program!";cout << "Hi, this is my program!";return 0;return 0;}}// The end of my program// The end of my program
continue…
Learners Support Publications www.lsp4you.com
Output OperatorOutput Operator
cout << "Hi, this is my program!";cout << "Hi, this is my program!";
cout is a predefined object that represents cout is a predefined object that represents the standard output stream in C++, here the standard output stream in C++, here the standard output stream is screen.the standard output stream is screen.
screen
cout << C++ variable
Object Insertion Operator
Learners Support Publications www.lsp4you.com
Output OperatorOutput Operator
The operator << is called The operator << is called insertioninsertion or or put to put to operatoroperator..
It inserts ( or sends ) the contents of the variable on It inserts ( or sends ) the contents of the variable on its right to the object on its left.its right to the object on its left.
cout << "Hi, this is my program!";cout << "Hi, this is my program!";
cout << number ;cout << number ;
screen
cout << C++ variable
Object Insertion Operator
continue…
Learners Support Publications www.lsp4you.com
Input OperatorInput Operator
cin >> number1;cin >> number1;
cin is a predefined object that corresponds cin is a predefined object that corresponds to the standard input stream in C++, here to the standard input stream in C++, here the standard input stream is keyboard.the standard input stream is keyboard.
cin >> 50.55
Object Extraction Operator C++ variable
Keyboard
Learners Support Publications www.lsp4you.com
Input OperatorInput Operator
The operator >> is known as The operator >> is known as extractionextraction or or get fromget from operator. operator.
It extracts ( or takes ) the value from the It extracts ( or takes ) the value from the keyboard and assigns it to the variable on its keyboard and assigns it to the variable on its right.right.
cin >> 50.55
Object Extraction Operator C++ variable
Keyboard
continue…
Learners Support Publications www.lsp4you.com
Cascading of I/O Cascading of I/O OperatorsOperators
Multiple use of << or >> in one Multiple use of << or >> in one statement is called cascading.statement is called cascading.
eg:-eg:-
cout << “Sum = ” << sum << cout << “Sum = ” << sum << “\n”;“\n”;
cin >> number1 >> number2;cin >> number1 >> number2;
Learners Support Publications www.lsp4you.com
Structure of a C++ Structure of a C++ ProgramProgram
Better to organize a program file into three Better to organize a program file into three separate files.separate files.
o Class declarations in a header file.Class declarations in a header file.o Definition of member function in a separate file.Definition of member function in a separate file.o The main program that uses the class is placed in The main program that uses the class is placed in
a third file which includes the previous two files.a third file which includes the previous two files.
Include files
Class declaration
Member functions declaration
Main function program