Download presentation
Presentation is loading. Please wait.
1
Review of C++ Language Basics
B.Ramamurthy CS114B 4/10/2019 B.Ramamurthy
2
The C++ Language C++ is a complex language designed by Bjarne Stroustup of At&T Bell Labs, Murray Hill, NJ. It evolved from the very popular C language (this was also designed at AT&T) out the need for object-orientation. (Unix is implemented in C) 4/10/2019 B.Ramamurthy
3
State of C++ In its author’s words, C++ is
a better C supports data abstraction supports object-oriented programming Current version of C++ is very robust, ANSI standardized, and widely used. Many large industrial programs are written in C++. 4/10/2019 B.Ramamurthy
4
“Out of Their Minds” “We did not know what we wanted and how to do it. It just sort of grew. The first struggle was over what the language would look like. Then how to parse the expressions….” John Backus (Inventor of FORTRAN, BNF (Backus-Naur Form) and FP, a functional language) 4/10/2019 B.Ramamurthy
5
Problem solving Problem solving involves understanding a problem, analyzing the problem and providing a solution to the problem. The solution specifies the overall design (structure chart, class diagrams, flow chart), algorithms and data organization. Phases of software development: problem statement, design solution, code the solution, document, debug, test, and maintain. 4/10/2019 B.Ramamurthy
6
Elements of C++ Every data (entity) used in an application should have a name (identifier) and type. A data type defines the set of values an object of this type can take and the operations that are allowed on this set. Example : Consider a whole number: Set of values => to Operations ==> + , - , * , / 4/10/2019 B.Ramamurthy
7
Some C++ -defined data types
Integer : int Real number : float character : char Boolean /logic : bool (available only in ANSI C++) No type possible? Use void 4/10/2019 B.Ramamurthy
8
Identifiers To identify the various data objects, programs, functions, and other items referenced by a program. Example : Grade1 , R_2D2 , Salary Rules for choosing an identifier: An identifier should always begin with a letter. This letter can be followed by any combination of {letter, digit, underscore} C++ reserved words cannot used an user-defined identifier. Reserved words are predefined in the language (C++) and cannot be used for anything other than the purpose for which they are reserved. 4/10/2019 B.Ramamurthy
9
Constants Constant : Refers to an object whose value remains constant throughout the execution of a program. Constant definition : Syntax : const type Constant_identifier = value; Example : const float pi = ; 4/10/2019 B.Ramamurthy
10
Operators - Arithmetic and Relational
Arithmetic: (integer) + addition - subtraction * multiplication / division (quotient only) % modulus (remainder of integer division) See Fig. 1.15 Relational < less than <= less than or equal to > greater than >= greater than or equal == equal to != not equal to 4/10/2019 B.Ramamurthy
11
Assignment Statement Syntax: Variable = expression; Semantics:
Evaluate the expression on the right hand side (RHS) of the = and assign the result to the variable on the left hand side (LHS). The expression on the RHS should be consistent with the type of the variable on the LHS. Expression represents a formula. It could be a constant, variable or combination of variables, constants and operators. EX: Total = Score1 + Score2; 4/10/2019 B.Ramamurthy
12
C++ Libraries C++ offers a collection of libraries of commonly used functions. A library is presented to the user/application program in the form of a header file. Example: math.h, iostream.h, thread.h To use the functions offered by a library, the header file is included using #include and functions in the library can be invoked like any other function. 4/10/2019 B.Ramamurthy
13
Program Input Syntax : cin input_list;
input_list consists of variables separated by extraction-operator >> The order of data must correspond to the order of the variables in the input-list. Semantics : Values for the variables specified are input from input device (stream). 4/10/2019 B.Ramamurthy
14
Program Input Example: int age; char first_initial;
cin >> age >> first_initial; Age first_initial 8 7 K ……………….. Input Stream (buffer) 4/10/2019 B.Ramamurthy
15
Program output Syntax : cout output_list;
output_list is a list of any combination of variables, constants, or strings each of which must be preceded by the insertion operator << Semantics: Each value is displayed in the order in which it is specified in the output_list; Example: cout << “My profit and initials are” << Net_Profit << Initial; 4/10/2019 B.Ramamurthy
16
Program Structure A C++ program is a collection of one or more functions. Every program has one function called main. To start the program execution, the Operating System transfers control to main. A return statement transfers control from a function to the caller of the function. 4/10/2019 B.Ramamurthy
17
Program Structure (contd.)
// Name of file // Author and other program-related information #include directives int main () { constant declarations variable declarations executable statements } 4/10/2019 B.Ramamurthy
18
Program Structure : Example
#include <iostream.h> // inclusion facility int main() { const float PI = 3.14; float Radius; cout << “Enter radius of the sphere \n”; cin >> Radius; float Volume = 4 * PI * Radius * Radius * Radius /3; cout << “The volume of the sphere of radius” << radius << “is” << Volume << endl; return 0; } 4/10/2019 B.Ramamurthy
19
Generating Executable Code
Type in program Editor emacs /vi /pico C++ Source Code sample.cc Compiler g++ -c sample.cc Object Code sample.o External References Libraries Linker g++ -o sample sample.o Executable code sample 4/10/2019
20
Summary We studied: Elements of C++ : identifiers, variables and constants, assignments, input/output statements Simple application program structure A complete application example 4/10/2019 B.Ramamurthy
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.