+ All Categories
Home > Education > Complete C++ programming Language Course

Complete C++ programming Language Course

Date post: 09-Jan-2017
Category:
Upload: vivek-chan
View: 661 times
Download: 6 times
Share this document with a friend
568
www.cyberlabzone.com C++ Programming
Transcript

C++ Programming

C++ Programming

www.cyberlabzone.com

1

Topics for C++ Language1.Introduction to C++ language2.Structured/OO Programming3.Memory Concepts4.Arithmetic5.Control Structure6.Functions7.Arrays8.Pointers9.Structures10.Inheritance11.Polymorphism12.Multiple Inheritance13.Templetes14.Stacks/Queue15.Exception Handling16.Preprocessing17.Unified Modeling Language

2

Introduction to C++ languageCourse DescriptionAn introduction to the development of programs using C++. Emphasis is given to the development of program modules that can function independently. Object-oriented designThe theory of data structures and programming language design is continued. Course Information

3

Computer LanguagesMachine languageGenerally consist of strings of numbers - Ultimately 0s and 1s - Machine-dependentExample:+1300042774+1400593419Assembly languageEnglish-like abbreviations for elementary operations Incomprehensible to computers - Convert to machine languageExample: LOADBASEPAYADD OVERPAYSTORE GROSSPAYHigh-level languages Similar to everyday English, use common mathematical notationsCompiler/InterpreterExample:grossPay = basePay + overTimePay

4

History of C and C++History of CEvolved from two other programming languagesBCPL and B: Typeless languagesDennis Ritchie (Bell Lab): Added typing, other features1989: ANSI standard/ ANSI/ISO 9899: 1990History of C++ Early 1980s: Bjarne Stroustrup (Bell Lab)Provides capabilities for object-oriented programmingObjects: reusable software components Object-oriented programsBuilding block approach to creating programsC++ programs are built from pieces called classes and functionsC++ standard library: Rich collections of existing classes and functions

5

Structured/OO ProgrammingStructured programming (1960s)Disciplined approach to writing programsClear, easy to test and debug, and easy to modifyE.g.Pascal:1971: Niklaus WirthOOP Software reuseModularity ExtensibleMore understandable, better organized and easier to maintain than procedural programming

6

Basics of a Typical C++ EnvironmentC++ systemsProgram-development environmentLanguageC++ Standard LibraryC++ program names extensions.cpp.cxx.cc.C

7

Basics of a Typical C++ EnvironmentPhases of C++ Programs:EditPreprocessCompileLinkLoadExecute

Loader

PrimaryMemory

Program is created inthe editor and storedon disk.

Preprocessor programprocesses the code.

Loader puts programin memory.

CPU takes eachinstruction andexecutes it, possiblystoring new datavalues as the programexecutes.

Compiler

Compiler createsobject code and storesit on disk.

Linker links the objectcode with the libraries,creates an executable file and stores it on disk

Editor

Preprocessor

Linker

CPU

PrimaryMemory

...

...

...

...

Disk

Disk

Disk

Disk

Disk

8

Basics of a Typical C++ EnvironmentCommon Input/output functionscinStandard input streamNormally keyboardcoutStandard output streamNormally computer screencerrStandard error streamDisplay error messagesComments: Cs comment /* .. */ OR Begin with // or Preprocessor directives: Begin with #Processed before compiling

9

A Simple Program: Printing a Line of TextStandard output stream objectstd::coutConnected to screen // or template < typename T >11 T maximum( T value1, T value2, T value3 ) 12 { 13 T max = value1; 14 15 if ( value2 > max ) 16 max = value2; 17 18 if ( value3 > max ) 19 max = value3; 20 21 return max; 22 23 } // end function template maximum 24

Formal type parameter T placeholder for type of data to be tested by maximum.

maximum expects all parameters to be of the same type.

61

25 int main()26 {27 // demonstrate maximum with int values28 int int1, int2, int3;29 30 cout > int1 >> int2 >> int3;32 33 // invoke int version of maximum34 cout > double2 >> double3;42 43 // invoke double version of maximum44 cout > char2 >> char3;52 53 // invoke char version of maximum54 cout = 0 && choice < 3 ) {27 28 // invoke function at location choice in array f29 // and pass choice as an argument 30 (*f[ choice ])( choice ); 31 32 cout > choice;34 }35 36 cout 40 hours] pays time and a half)Commission (paid percentage of sales)Base-plus-commission (base salary + percentage of sales)Boss wants to raise pay by 10%

312

Payroll System Using PolymorphismBase class EmployeePure virtual function earnings (returns pay)Pure virtual because need to know employee typeCannot calculate for generic employeeOther classes derive from Employee

Employee

SalariedEmployee

HourlyEmployee

CommissionEmployee

BasePlusCommissionEmployee

313

Dynamic CastDowncastingdynamic_cast operatorDetermine object's type at runtimeReturns 0 if not of proper type (cannot be cast)NewClass *ptr = dynamic_cast < NewClass *> objectPtr;Keyword typeidHeader Usage: typeid(object)Returns type_info objectHas information about type of operand, including nametypeid(object).name()

314

1 // Example 612 // Employee abstract base class.3 #ifndef EMPLOYEE_H4 #define EMPLOYEE_H5 6 #include // C++ standard string class7 8 using std::string;9 10 class Employee {11 12 public:13 Employee( const string &, const string &, const string & );14 15 void setFirstName( const string & );16 string getFirstName() const;17 18 void setLastName( const string & );19 string getLastName() const;20 21 void setSocialSecurityNumber( const string & );22 string getSocialSecurityNumber() const;23

315

24 // pure virtual function makes Employee abstract base class25 virtual double earnings() const = 0; // pure virtual26 virtual void print() const; // virtual27 28 private:29 string firstName;30 string lastName;31 string socialSecurityNumber;32 33 }; // end class Employee34 35 #endif // EMPLOYEE_H

316

1 // Example 622 // Abstract-base-class Employee member-function definitions.3 // Note: No definitions are given for pure virtual functions.4 #include 5 6 using std::cout;7 using std::endl;8 9 #include "employee.h" // Employee class definition10 11 // constructor12 Employee::Employee( const string &first, const string &last,13 const string &SSN )14 : firstName( first ),15 lastName( last ),16 socialSecurityNumber( SSN )17 {18 // empty body 19 20 } // end Employee constructor21

317

22 // return first name23 string Employee::getFirstName() const 24 { 25 return firstName; 26 27 } // end function getFirstName28 29 // return last name30 string Employee::getLastName() const31 {32 return lastName; 33 34 } // end function getLastName35 36 // return social security number37 string Employee::getSocialSecurityNumber() const38 {39 return socialSecurityNumber; 40 41 } // end function getSocialSecurityNumber42 43 // set first name44 void Employee::setFirstName( const string &first ) 45 { 46 firstName = first; 47 48 } // end function setFirstName49

318

50 // set last name51 void Employee::setLastName( const string &last )52 {53 lastName = last; 54 55 } // end function setLastName56 57 // set social security number58 void Employee::setSocialSecurityNumber( const string &number )59 {60 socialSecurityNumber = number; // should validate61 62 } // end function setSocialSecurityNumber63 64 // print Employee's information65 void Employee::print() const66 { 67 cout = 0.0 && hoursWorked 0.0 && rate < 1.0 ) ? rate : 0.0;45 46 } // end function setCommissionRate47

329

48 // calculate commission employee's earnings49 double CommissionEmployee::earnings() const50 { 51 return getCommissionRate() * getGrossSales(); 52 53 } // end function earnings54 55 // print commission employee's name 56 void CommissionEmployee::print() const57 {58 cout ( numerator ) / denominator;36 37 } // end function quotient38 39 int main()40 {41 int number1; // user-specified numerator42 int number2; // user-specified denominator43 double result; // result of division44 45 cout > number1 >> number2 ) {49 50 // try block contains code that might throw exception 51 // and code that should not execute if an exception occurs52 try { 53 result = quotient( number1, number2 ); 54 cout = 0 && choice < 3 ) {27 28 // invoke function at location choice in array f29 // and pass choice as an argument 30 (*f[ choice ])( choice ); 31 32 cout > choice;34 }35 36 cout


Recommended