Chapter 13 Introduction to C Language - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 13 Introduction to C Language

Description:

Type language (stronger type than C) Variable must be declared a type ... Problem: Write a class Dice to simulate. flipping two dices. Example 2. Slide 22 ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 25
Provided by: radf7
Category:

less

Transcript and Presenter's Notes

Title: Chapter 13 Introduction to C Language


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 -gt C Compiler
  • -gt Object program -gt Link
  • -gt(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
  • then
  • 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 ltiostreamgt

8
Program Structures (Cont.)
  • // all C library declaration starts with c
  • include ltcmathgt //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!

9
Program Structures (Cont.)
  • // declare types
  • typedef char myCharacter
  • // declare global variables
  • int a
  • char ba, c\n
  • myCharacter e
  • long d 14000L // this means long int (default
    int)
  • float x-2.5F
  • double y2.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 ltlt lb ltltendl
  • return 0 // no error exit

11
Program Structures (Cont.)
  • // define function
  • int sub1 (int l)
  • // call function
  • int la 5l
  • return la

12
Variable
  • Variable declaration
  • Form type variable initial value
  • e.g. int number
  • Pointer variable declaration
  • Form type variable initial value
  • e.g. int numberptr0
  • // numberptr is initialized to null pointer

13
Constant Variable
  • Constant variable declaration
  • Form const type variable
  • e.g. const double PI 3.14159
  • 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 aliasvariable
  • e.g. int number10
  • int numberaliasnumber

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_castltleft hand side typegt
    (expression)
  • Example int number static_castltintgt(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 i0ilt5i)
  • cout ltlt TRUE is printed!\n // print 5
    times

19
Format Data Stream
  • Need include ltiomanipgt
  • setw() set total of spaces to print
  • if not enough, give exact of spaces needed
  • setiosflags(iosfixediosshowpoint)
  • print decimal point
  • setprecision()
  • set total of spaces after decimal point
  • format.cc

20
Example without Using Class
  • Example 1
  • 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
Write a Comment
User Comments (0)
About PowerShow.com