Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.

Similar presentations


Presentation on theme: "Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming."— Presentation transcript:

1 Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming

2 Procedural Programming
Earlier programming approach intuitive task based Problem was broken into manageable sub-tasks Code was written to accomplish each sub-task Resulting “functions” assembled to solve overall problem Like a recipe

3 Object Oriented Programming
Newer programming approach Data based rather than task based Abstract objects are created representing the data elements to be worked with Each object is self-contained Storing both data and procedures for manipulating the data Details hidden from the user – i.e. a “black-box” Objects are manipulated to solve a given problem Advantage: “black-box” localizes software changes Large software projects are more manageable and supportable

4 C++ Language Elements Comments Compiler directives Function main
/* miles.cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() // start of main function { const float km_per_mile = 1.609; // km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i.e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; return 0; } Comments Compiler directives Function main Declaration statements Executable statements Example: miles.cpp

5 Comments /* miles.cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() // start of main function { const float km_per_mile = 1.609; // km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i.e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; return 0; } // symbols indicate a line comment - apply to just the rest of the line Block comments start with /* and end with */ - apply to as many lines as you like Used to describe the code in English or provide non-code information E.g. to include the name of the program or the author’s name

6 #include Compiler directive
/* miles.cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() // start of main function { const float km_per_mile = 1.609; // km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i.e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; return 0; } Compiler directive Includes previously written code from a library into your program Libraries allow for code reuse E.g. #include <iostream> has operators for performing input and output within the program The compiler knows where to locate libraries enclosed in <> iostream will be needed in our programs in order to use cin and cout, as shown …

7 using namespace std; Indicates to compiler that this program uses objects defined by a standard namespace called std. i.e. a portion of the iostream library Ends with a semicolon Follows #include directives in the code /* miles.cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() // start of main function { const float km_per_mile = 1.609; // km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i.e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; return 0; }

8 Function main() Exactly one main function per program
/* miles.cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() // start of main function { const float km_per_mile = 1.609; // km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i.e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; return 0; } // end of main function Exactly one main function per program A function is a collection of related statements that perform a specific set of tasks int indicates the return type of the function ( ) any input to the function would normally go here empty parenthesis indicate that no special information is being passed to the function by the operating system

9 Types of Statements All C++ statements end with “;”
A single statement may span multiple lines Two main types of statements Declaration statements describe the data the function needs: const float KM_PER_MILE = 1.609; float miles, kms; (note: this is all one line!) Executable statements specify actions the program will take cout << “Enter the distance in miles: “; cin >> miles; /* miles.cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() // start of main function { const float km_per_mile = 1.609; // km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i.e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; return 0; }

10 Reserved Words (Keywords)
Have special meaning in C++ Cannot be used for other purposes /* miles.cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() // start of main function { const float km_per_mile = 1.609; // km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i.e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; return 0; }

11 Identifiers Names for data and objects (variables) to be manipulated by the program Must begin with a letter or underscore (not recommended) Consist only of letters, digits, and underscores Cannot be reserved word Upper and lower case significant miles.cpp cin, cout, kms, km_per_mile, miles, std, endl /* miles.cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() // start of main function { const float km_per_mile = 1.609; // km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i.e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; return 0; }


Download ppt "Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming."

Similar presentations


Ads by Google