Unformatted text preview:

C++ BasicsC++ OverviewIdentifiersSlide 4Typical size of data typesFinding size of data typeEnumerated types: enumBoolean typeSlide 9Named Constantssome keywords: reserved to C++two portable versions of mainSlide 13Modulus OperatorPrefix vs postfixSlide 16What’s a file?Operators can beSome C++ OperatorsType Casting is Explicit Conversion of Type>> is a binary operatorSome ExpressionsParts of a FunctionHEADER FILE FUNCTION EXAMPLE VALUE OF CALLSlide 25output of float may look like int!scientific notationCan take control of formattingC++ basicsInput StatementsWhitespace Characters Include . . .Extraction Operator >>Another way to read char dataAt keyboard you type: A[space]B[space]C[Enter]C++ BasicsArithmetic expressionsC++ control structuresCONTROL STRUCTURESSlide 39“SHORT-CIRCUIT” EVALUATIONShort-Circuit ExampleBetter exampleCompound statementConditional statementsBeware of dangling elseIteration statementsSlide 47Slide 48BreakCombining break and forSwitchSlide 52What’s the output?Conditional expressionsConditional expressionSimple arraysType safetyFunctions: 3 parameter transmission modesFunctions: example of pass by valueFunctions: example of pass by referenceFunctions: pass by const referenceArrays are passed by referenceThe void keywordFunctions: Types of arguments and return valuesFunctions: Types of arguments and return valuesFunctions: initializationA static variable can be used as a flagSlide 68Slide 69Function overloadingFunctions: overloadingFunctions: References as return valuesFunctions: Inline functions and macrosWhat’s better: #define, or constconstants in a class:constants in a class (old style):Which is safer: macro or inline? which faster? which smaller?Use inlining judiciouslyWhich is better printf or cout?What’s the difference between <iostream.h> and <iostream>What’s the difference between string.h and stringWhat’s the algorithm for old/newPreprocessor facilitiesSlide 84C strings:C-string exercise:Command line parametersSlide 88Slide 89Reading from filesTo Use Disk I/O, you mustStatements for using Disk I/OWhat does opening a file do?Slide 94Slide 95Slide 96Exercises: write a program that reads from a file andC++ & Object-Oriented ProgrammingC++ BasicsBrian Malloy, PhDDepartment of Computer ScienceClemson UniversityClemson SC, USA2C++ OverviewDesigned by B. Stroustrup (1986),C++ and ANSI C Hybrid language: OO and ‘conventional’ programming,More than just an OO version of Coperator overloadingtemplates3IdentifiersAn identifier must start with a letter or underscore, and be followed by zero or more letters C++ is case sensitiveC++ is case sensitive VALID age_of_dog TaxRate98PrintHeading AgeOfHorse4C++ Data TypesC++ Data TypesStructuredarray struct union class Addresspointer referenceSimple Integral Floatingchar short int long enumfloat double long double5Typical size of data types Type Number of Byteschar 1short (short int) 2int 4unsigned int 4 enum 2long (long int) 4float 4double 8long double 126Finding size of data typeUse sizeofsizeof to find the size of a typee.g.cout << sizeof(int)7Enumerated types: enumUsed to define constant values enum days{ Sunday = 0, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday} yesterday, today;8Boolean typeC++ has a built-in logical or Boolean typebool flag = true;9Giving a value to a variableIn your program you can assign (give) a value to the variable by using the assignment operator = int Age_Of_Dog = 12;or by another method, such ascout << “How old is your dog?”;cin >> Age_Of_Dog;10Named ConstantsA named constant is a location in memory which we can refer to by an identifier, and in which a data value that cannot be changed is stored. VALID CONSTANT DECLARATIONS const char STAR = ‘*’ ; const float PI = 3.14159 ; const int MAX_SIZE = 50 ;11some keywords: reserved to C++ bool, true, false, char, float, int, unsigned, double, long if, else, for, while, switch, case, break,  class, public, private, protected, new, delete, template, this, virtual, try, catch, throw.12two portable versions of mainint main ( ) { return 0;}type of returned valuename of functionsays no parameters13two portable versions of mainint main (int argc, char * argv[] ) { return 0;}14Modulus OperatorThe modulus operator % can only be used with integer type operands and always has an integer type result.Its result is the remainder after divisionEXAMPLE11 % 4 is ?)411R = ?15Prefix vs postfixWhen the increment (or decrement) operator is used in a statement with other operators, the prefix and postfix forms can yield different results.16Program with Three Functions#include <iostream>int Square ( int ) ; // declares these 2 functionsint Cube ( int ) ;int main ( void ) { cout << “The square of 27 is “ << Square (27) << endl; cout << “The cube of 27 is “ << Cube (27) << endl ;}int Square ( int n ) { return n * n ; }int Cube ( int n ) { return n * n * n ; }17What’s a file?A logical structurecontains data in some formatasciibinaryjpeg, gifprovides logical structure to the datausually stored on disk18Operators can bebinary involving 2 operands 2 + 3unary involving 1 operand - 3ternary involving 3 operands ?:19Some C++ OperatorsPrecedence Operator Description Higher ( ) Function call + Positive - Negative * Multiplication / Division % Modulus (remainder) + Addition - SubtractionLower = Assignment20Type Casting is Explicit Conversion of Typeint(4.8) has valuestatic_cast<float>(5)float(2/4)float(2) / float(4)21>> is a binary operator>> is called the input or extraction operator>> is left associativeEXPRESSION HAS VALUEcin >> Age cinSTATEMENTcin >> Age >> Weight ;22Some Expressionsint age;EXAMPLE VALUEage = 85 / 86.0 / 5.0float ( 4 / 8 )float ( 4 ) / 8cout << “How old are you?” cin >> agecout << age23Parts of a FunctionEvery function has 2 partsint main (void) signature{ body return 0;}24HEADER FILE FUNCTION EXAMPLE VALUE OF CALL fabs(x) fabs(-6.4) 6.4<cmath> pow(x,y) pow(2.0,3.0) 8.0<cmath> sqrt(x) sqrt(100.0) 10.0<iomanip> setprecision(n) setprecision(3)<cmath> log(x) log(2.0) .693147 sqrt(x) sqrt(2.0) 1.41421<stdlib.h> abs(i) abs(-6) 6C++ BasicsFormatted I/O2526output of


View Full Document

Clemson CPSC 870 - C++ Basics

Documents in this Course
Load more
Download C++ Basics
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C++ Basics and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C++ Basics 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?