Download presentation
Presentation is loading. Please wait.
1
Chapter 13 Introduction to C++ Language
By C. Shing ITEC Dept Radford University
2
Objectives Understand C++ features Understand Program Structure in C++
3
C++ Features Type language (stronger type than C)
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 Overloading (operator and function): same function/operator name with different argument list Inheritance and Polymorphism: virtual function Template Same function task for different data types
4
Running C++ Program C source program -> C++ Compiler
-> Object program -> Link ->(feed data) Excution
5
Program Structures File Extension
.cc (or .cpp) Compile & link (Solaris) , then execute g++ mainfile.cc [sub1filename.cc sub2filename.cc] then a.out or g++ -o executable mainfile.cc [sub1filename.cc sub2filename.cc] executable
6
Program Structures (Cont.)
Compile only (Solaris) g++ –c filename.cc
7
Program Structures (Cont.)
mainfile.cc /* ********************************** * This is a comment * * Block * */ // Comment line // Declaration for standard library // The following line is required #include <iostream>
8
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 = ; const char MY_MESSAGE[] = “Good Morning!”;
9
Program Structures (Cont.)
// declare types typedef char myCharacter; // declare global variables int a; char b=‘a’, c=‘\n’; myCharacter e; long d = 14000L; // this means long int (default int) float x=-2.5F; double y=2.5;
10
Program Structures (Cont.)
// declare function prototype int sub1(int); // define main function int main () { int lc = 2; // call function int lb = sub1(lc); cout << lb <<endl; return 0; // no error exit }
11
Program Structures (Cont.)
// define function int sub1 (int l) { // call function int la = 5*l; return la; }
12
Variable Variable declaration Pointer variable declaration
Form: type variable [=initial value]; e.g. int number; Pointer variable declaration Form: type *variable [=initial value]; e.g. int* numberptr=0; // numberptr is initialized to null pointer
13
Constant Variable Constant variable declaration
Form: const type variable; e.g. const double PI = ; Constant address (or pointer variable) declaration Form: pointer type const variable; e.g. double * const PIptr;
14
Variable (Cont.) Constant address points to constant variable
declaration Form: const pointer type const variable; e.g. const double * const PIptr; Alias or reference variable (must initialize when declare it) Form: type &alias=variable; e.g. int number=10; int &numberalias=number
15
Difference Between Alias and Pointer
Alias must have variable value (not null). Pointer can be null. Alias will not be reassigned once declared, only the value will be changed Alias can access the variable directly, pointer accesses the variable indirectly
16
Difference Between Alias and Pointer (Cont.)
Example: ref.cc
17
Assignment Statement Casting:
Identifier = static_cast<left hand side type> (expression) Example: int number = static_cast<int>(3.1416); Then number contains 3
18
Loop Statement – for (Cont.)
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 }
19
Format Data Stream Need #include <iomanip> format.cc
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
20
Example without Using Class
hello.cc hello1.cc hello2.cc
21
Program Structures (Using Class)
Problem: Write a class Dice to simulate flipping two dices. Example 2
22
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
23
Compare C++ to Java Program
Problem: Write a class Dice to simulate flipping two dices. Example 3 dice.java
24
Reference: Deitel & Deitel: C How to Program, 4th ed.,
Chapter 15, Prentice Hall
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.