Chapter 13 Introduction to C Language By C

Chapter 13 Introduction to C++ Language By C. Shing ITEC Dept Radford University

Objectives n n Understand C++ features Understand Program Structure in C++ Slide 2

C++ Features n Type language (stronger type than C) n n Overloading (operator and function): n n n Variable must be declared a type (declaration mixed with statements) has boolean data type bool (false, true) Class: hide member data (only member function can access them), provide public interface by member function same function/operator name with different argument list Inheritance and Polymorphism: virtual function Template n Same function task for different data types Slide 3

Running C++ Program C source program -> C++ Compiler -> Object program -> Link ->(feed data) Excution n Slide 4

Program Structures § File Extension § § . cc (or. cpp) Compile & link (Solaris) , then execute § g++ mainfile. cc [sub 1 filename. cc sub 2 filename. cc] then § a. out or § g++ -o executable mainfile. cc [sub 1 filename. cc sub 2 filename. cc] then § executable Slide 5

Program Structures (Cont. ) § Compile only (Solaris) § g++ –c filename. cc Slide 6

Program Structures (Cont. ) § mainfile. cc /* ***************** * This is a comment * * Block * ***************** */ // Comment line // Declaration for standard library // The following line is required #include <iostream> Slide 7

Program Structures (Cont. ) // all C library declaration starts with c #include <cmath> //if math library is used // The following file yourdeclaration. h // is in your current directory //#include “yourdeclaration. h” using namespace std; // for cin and cout // declare named global constants const int number = 4; const double PI = 3. 14159; const char MY_MESSAGE[] = “Good Morning!”; Slide 8

Program Structures (Cont. ) // declare types typedef char my. Character; // declare global variables int a; char b=‘a’, c=‘n’; my. Character e; long d = 14000 L; // this means long int (default int) float x=-2. 5 F; double y=2. 5; Slide 9

Program Structures (Cont. ) // declare function prototype int sub 1(int); // define main function int main () { int lc = 2; // call function int lb = sub 1(lc); cout << lb <<endl; return 0; // no error exit } Slide 10

Program Structures (Cont. ) // define function int sub 1 (int l) { // call function int la = 5*l; return la; } Slide 11
![Variable n Variable declaration Form: type variable [=initial value]; e. g. int number; n Variable n Variable declaration Form: type variable [=initial value]; e. g. int number; n](http://slidetodoc.com/presentation_image_h2/9ad7eca7b816672124ae3ec78250eba9/image-12.jpg)
Variable n Variable declaration Form: type variable [=initial value]; e. g. int number; n n Pointer variable declaration Form: type *variable [=initial value]; e. g. int* numberptr=0; // numberptr is initialized to null pointer n Slide 12

Constant Variable n Constant variable declaration Form: const type variable; e. g. const double PI = 3. 14159; n n Constant address (or pointer variable) declaration Form: pointer type const variable; e. g. double * const PIptr; n Slide 13

Variable (Cont. ) n Constant address points to constant variable declaration Form: const pointer type const variable; e. g. const double * const PIptr; n n Alias or reference variable (must initialize when declare it) Form: type &alias=variable; e. g. int number=10; int &numberalias=number n Slide 14

Difference Between Alias and Pointer Alias must have variable value (not null). Pointer can be null. n Alias will not be reassigned once declared, only the value will be changed n Alias can access the variable directly, pointer accesses the variable indirectly n Slide 15

Difference Between Alias and Pointer (Cont. ) Example: ref. cc Slide 16

Assignment Statement n Casting: n Identifier = static_cast<left hand side type> (expression) Example: int number = static_cast<int>(3. 1416); Then number contains 3 Slide 17

Loop Statement – for (Cont. ) n Example: // loop index can be defined inside for loop // and only meaningful within the loop for (int i=0; i<=5; ++i) { cout << “TRUE is printed!n”; // print 5 times } Slide 18

Format Data Stream n Need #include <iomanip> n n n setw(): set total # of spaces to print if not enough, give exact # of spaces needed setiosflags(ios: : fixed|ios: : showpoint): print decimal point setprecision(): set total # of spaces after decimal point format. cc Slide 19

Example without Using Class n n Example 1 hello. cc hello 1. cc hello 2. cc Slide 20

Program Structures (Using Class) Problem: Write a class Dice to simulate flipping two dices. Example 2 n Slide 21

Program Structures (Using Class)- Cont. Problem: Write a class Dice to simulate flipping two dices. (Cont. ) Use Modules: driver class implementation class specification header file n Slide 22

Compare C++ to Java Program Problem: Write a class Dice to simulate flipping two dices. Example 3 dice. java n Slide 23

Reference: Deitel & Deitel: C How to Program, 4 th ed. , Chapter 15, Prentice Hall n Slide 24
- Slides: 24