Title: Engineering Problem Solving with C
1Engineering Problem Solving with C
- Fundamental Concepts
- Chapter 8
- Introduction to C
2The C Programming Language
- A superset of C
- C compilers can be used to compile C programs
- Supports
- Object Oriented Programming (OOP)
- Templates
- Overloading of functions and operators
- Best to think of C as its own language
3Object Oriented Programming
- Object oriented programming is characterized by
the use of - classes
- objects
- inheritance
- polymorphism.
4Object Oriented Programming
- A class is a programmer defined data type that
combines data members and function members that
operate on the data members. - An object is a variable of a defined class type,
usually referred to as an instance of a class.
5Object Oriented Programming
- Inheritance allows a class to inherit attributes
from an existing class. - Polymorphism is the ability to assign many
meanings to the same name. - Overloading of functions is one example of
polymorphism in C.
6Basic C Program Structure
/
Header
Comments
/ global
declarations int main() declarations and
executable statements return 0 //end block of
main
7First Program
- Comments have two forms in C
- //Single line comments
- /Multi-line comments/
- include files
- ltfilename.hgt //Standard library header file
- myfile.h //Your files (need full path)
8First Program - sum two numbers
/
Sum two
numbers
/ include
ltiostream.hgt //declares standard I/O library int
main() //must have one and only one function
named main int number1, number2 //declare two
integer variables cout ltlt enter two integers
ltlt endl //prompt for input cin gtgt number1 gtgt
number2 //input values for two variables from
keyboard cout ltlt number1 number2 //output sum
of two numbers to the screen return 0 //end
block of main
9Simple I/O
- cin
- is an istream object
- streams input from standard input
- uses the gtgt (input operator)
- cout
- is an ostream object
- streams output to standard output
- uses the ltlt (output) operator
10File Streams
- cin
- object of type istream (an instance of the class
istream). - defined for you in file iostream.h
- defined to stream input from standard input
- some associated member functions
- eof()
- get()
11File Streams
- cout
- object of type ostream (an instance of the class
ostream). - defined for you in the file iostream.h
- defined to stream output to standard output
- some associated member functions
- put()
12Programmer Defined File Streams
13Programmer Defined File Streams
14Programmer Defined File Streams
- To define your own file streams
- for input use ifstream class
- for output use ofstream class
- ifstream and ofstream are defined in file
fstream.h - fstream.h also includes istream and ostream class
15File Streams
- ifstream is derived from istream
- includes all functions defined for cin - eof(),
get(), .. - Includes additional functions not defined for cin
- open(), close(), fail()
16File Streams
- ofstream is derived from ostream
- includes all functions defined for cout - put(),
.. - Includes additional functions not defined for
cout - open(), close(), fail()
17Defining File Streams
- Include fstream.h
- declare filestream objects
- ifstream fin
- ofstream fout
- use open() to initialize filestreams
- use filestreams just like cin and cout
18Example
include ltfstream.hgt include ltmath.hgt int
main() //Create three columns of data for
plotting. double x ifstream xdata //declare
input stream ofstream plotdata //declare output
stream xdata.open("exp1data" ) //file stream
xdata is now connected to file if( xdata.fail()
) //check for open error cout ltlt "error
opening input file" ltlt endl return 0 //end
if //Continued on next slide.
19plotdata.open("exp1plot) xdata gtgt
x while(!xdata.eof()) if( xgt0 )
//write to plot file plotdata ltlt x ltlt" " ltlt
exp(x) ltlt " " ltltlog(x) ltlt endl //end if
xdata gtgt x //end while xdata.close() plotdata.
close() return 0 //end main
20Introduction to Classes
- The building blocks of object oriented
programming - Similar to structures
- Include function members and data members
- A well designed class is as easy to use as a
pre-defined data type
21Designing a Class
- Required data members
- Required operations
- Constructor functions
- Accessor functions
- Helper functions
- Friend functions
- Overloaded Operators
22Writing a Class Definition
- A class definition has two parts
- Class declaration
- Class implementation
23Class Declaration
- Name of the class is specified using the key word
class - Body of the class declaration includes
- declaration statements for the data members
- function prototypes
- Keywords public and private specify the
accessibility of the class members
24Example - Date Class
- Class Declaration
- class Date
- //class body
- public
- void input(istream)
- void print(ostream)
- void set_data(int, int, int)
- private
- int day, month, year
-
25Date Class
- Name of the class is Date
- Three private data members
- Three public function members
- Data members can only be accessed by the member
functions
26Class Implementation
- Includes
- Function definitions for all function members
- Scope Resolution Operator () specifies a
function as a member of a class
27Implementation of Date Class
- void Dateinput(istream in)
-
- in gtgt month gtgt day gtgt year
-
- void Dateprint(ostream out)
-
- out ltlt month ltlt/ ltltday ltlt / ltlt year
-
- void Dateset_data(int m, int d, int y)
-
- month m
- day d
- year y
-
28Using Programmer Defined Classes
- Separate Compilation
- Class declaration is saved in a file named
classname.h - Class implementation is saved in a file named
classname.cpp(or whatever extension your compiler
expects) - .h file is included in user program and
implementation file - User program is linked to implementation file
29Using the Date Class
- include date.h
- int main()
-
- Date today, birthday
- //member functions are called using the dot
operator - today.set(4,6,2000)
- birthday.input(cin)
- today.print(cout)
- birthday.print(cout)
- //how do you determine if today is your birthday?
-
-
30Operators
- Assignment operator is defined for objects of the
same type - Date d1, d2
- d1.input(cin)
- d2 d1
- Other operators are not defined
- arithmetic, relational, logical, input and output
31Accessor Functions
- Required to access private data members
- Complete set should be provided
- Our Date class requires 3 accessor functions
32Accessor Function Prototypes
- int get_day()
- int get_month()
- int get_year()
33Practice!
- 1. Write the function definitions for each of
the accessor functions for the Date class.
34Initializing Objects
- Constructor functions are used to initialize
objects at the time they are declared - Called automatically
- Same name as the class name
- No return value
35Constructors for the Date Class
- Default Constructor
- constructor with no arguments
- Prototype
- Date()
- Definition
- DateDate()
-
- month1
- day1
- year2000
-
-
36Constructors With Parameters
- Prototype
- Date(int, int, int)
- Definition
- DateDate(int m, int d, int y)
-
- month m
- day d
- year y
-
37Overloading Operators
- Allows a programmer defined data type to be used
as easily as a predefined data type. - Operators are included in a class definition.
The keyword operator precedes the name of the
function. - The name of the function is one of the predefined
C operators - Only predefined operators may be overloaded
- (all operators except . . ? may be
overloaded)
38Complex Number Class
A complex number is a number that has two
components the real component and the imaginary
component. a bi Arithmetic on is defined as
follows (a bi) (c di) (a c) (b
d)i (a bi) - (c di) (a - c) (b - d)i (a
bi) (c di) (ac - bd) (ad bc)i (a
bi) / (c di) (ac bd) / (c2d2)
(bc -ad) /(c2d2)i
39Class Declaration
class complex public complex()
void print(ostream) void
input(istream) complex
operator(complex) complex
operator-(complex) complex
operator(complex) complex
operator/(complex) private double real,
imag
40Implementation
complexcomplex() //default constructor
realimag0 complex complexoperator(comple
x c) complex temp temp.real
c.real real temp.imag c.imag
imag return temp
41Implementation - Continued
complex complexoperator/(complex c)
complex temp temp.real (realc.real
imagc.imag)/( pow(c.real,2) pow(c.imag,2)
) temp.imag (imagc.real -
realc.imag)/( pow(c.real,2) pow(c.imag,2)
) return temp
42Test Program
complex c1, c2, c3 //declare three complex
variables c1.input(cin) c2.input(cin) //test
addition c3 c1 c2 cout ltlt endl ltlt "c1 c2
is " c3.print(cout) //test division c3 c1 /
c2 cout ltlt endl ltlt "c1 / c2 is
" c3.print(cout) cout ltlt endl
43Sample Output
for input 4.4 1.5 3.5 -2.5 The output from our
test program will be c1 c2 is 7.9 -1i c1 /
c2 is 0.62973 0.878378i