Title: CS304: Lecture 2
1CS304 Lecture 2
- Basics of C Programming
- Deitel, Ch. 1-5
- http//www.cis.uab.edu/cs304
2Outline
- Using Datatypes
- Integers
- Char
- Arithmetic Operations
- Relational Operators
- Global functions
- Class Overview
- Data Members
- Public vs. Private
- Member Functions
- Setters and Getters (Why?)
- Constructors
- Implicit vs. Declared
- Header Files
- Separating Interface and Implementation
- Control Structures
- If, if-else, conditional operator (?), while,
do-while, for, switch
3Ladies and Gentlemen, Start Your Compilers!
- include ltiostreamgt
- using stdcout //Whats up with that ?
- int main()
-
- cout ltlt "Hello, world!\n"
- return 0
4Namespaces, a Conceptual View
namespace std //classes //functions //globals
namespace myspace //classes //functions //glo
bals
namespace oracle //classes //functions //glob
als
5Ladies and Gentlemen, Start Your Compilers!
- include ltiostreamgt
- using stdcout
- int main()
-
- cout ltlt "Hello, world!\n"
- // Whats up with that ltlt?
- return 0
6Streams and their Operators
- Several streams are already available
- cout Standard Output
- cin Standard Input
- cerr Standard Error
- Operators
- ltlt -- Put onto the stream
- gtgt -- Take off of the stream
- Put stuff onto cout, take stuff off of cin
7Datatypes A Review
- Numbers int, float, double
- Qualifiers short or long, signed or unsigned
- Size in bytes (on the blazers)
- char 1 (What is a char, anyway?)
- short int 2
- int 4
- long int 4
- long long int 8 (dont try this at home)
- float 4
- double 8
- long double 16
8A Silly Example
- include ltiostreamgt
- using namespace std
- int main()
-
- int a short int b long int c
- float d double e long double f
- char g
- cin gtgt a gtgt b gtgt c gtgt d gtgt e gtgt f gtgt g
- cout ltlt a ltlt b ltlt c ltlt d ltlt e ltlt f ltlt g
- return 0
9Arithmetic and Relational Operations
- Arithmetic The standards , -, /, , , ()
- They work the way Java programmers expect
- Relational gt, gt, lt, lt, , !
- Same goes
- Thirty seconds, and I have already spent too much
time on this!
10Functions
- Functions can be a part of a class
- They can also be declared in a global scope
- Globals in this case arent necessarily a bad
thing - Ever seen someone declare a global class in
java for misc. static functions? - The interface can be declared before the function
itself - This is a good practice
11Another Example
- include ltiostreamgt
- using namespace std
- int mymin(int a, int b)
- int main()
-
- int a, b
- cout ltlt "Please enter two numbers "
- cin gtgt a gtgt b
- cout ltlt mymin(a, b)
-
- int mymin(int a, int b)
-
- if (a lt b) return a
- return b
-
12Classes in C
- Work a lot like in Java, with less typing!
- Not necessary to assign a new instance at
declaration - Treated more like a primitive datatype
13Class Example
- include ltiostreamgt
- include ltstringgt
- using namespace std
- class name
- public
- string firstname
- string lastname
- void printName() cout ltlt firstname ltlt " " ltlt
lastname ltlt endl -
- int main()
- name MyName
- MyName.firstname "Matthew"
- MyName.lastname "Curry"
- MyName.printName()
- return 0
14Another Example Functions, Getters, Setters
- include ltiostreamgt
- include ltstringgt
- using namespace std
- class name
- public
- void setfirstname(string firstname)
this-gtfirstname firstname - string getfirstname() return firstname
-
- private
- string firstname
- string lastname
15Creating a Header File
- Too much code in one place
- Separation of unrelated code
- include rears its ugly head once again
16Header file example
- main.cpp
- include name.h
- include ltiostreamgt
- using namespace std
- int main()
-
- name myname
- myname.setfirstname("Matthew")
- myname.setlastname("Curry")
- cout ltlt "My first name is " ltlt
myname.getfirstname() ltlt endl - cout ltlt "My last name is " ltlt myname.getlastnam
e() ltlt endl - return 0
-
name.hinclude ltiostreamgtusing namespace
std class name public void
setfirstname(string firstname)
this-gtfirstname firstname
string getfirstname() return
firstname private string
firstname string lastname
17Further Separation
- name.h
- include ltstringgt
- using namespace std
- class name
- publicvoid setfirstname(string
firstname)string getfirstname() - privatestring firstnamestring lastname
-
name.cpp include name.h namesetfirstname(str
ing firstname) this-gtfirstname
firstname namegetfirstname() return
firstname
18Control Structures
- Much like Java, thankfully
- If, if-else
- for, while, do-while,
- conditional operator
- switch
19If, If-Else
- If (condition)
- //do stuff
- else
- //do something else
-
- Remember An else matches the closest unmatched
if!
20Conditional Operator
- ? is just a quick way to say if-else within an
expression - include ltiostreamgt
- using namespace std
- // The ugliest code youve ever seen
- int main()
-
- int x
- string count
- cin gtgt count
- count (x 0) ? "0" "many"
- cout ltlt "x " ltlt count ltlt endl
21For
- Syntax for (initialize test loop operation)
- for( ) creates an infinite loop
- for( conditional ) mimics while
- Scope works the same as in java Declare a
counter within the for loop to be used within - for (int x 0 x lt 1000 x) cout ltlt
This is annoying!\n - The break command exits the loop prematurely,
while the continue command skips to the end of
the loop
22While, Do-While
- int x 0
- while ( x lt 1000 )
- cout ltlt This is annoying, too!\n
-
- x 0
- do
- cout ltlt Shall I continue?\n
- while (x lt 1000)
- //Breaks and continues work here, too
23Switch
- Switch is a replacement for huge if-else chains.
- switch(x)
- case 0
- case 1
- cout ltlt "Too few!\n"
- break
- case 2
- cout ltlt "Just right!\n"
- break
- default
- cout ltlt "Too many!\n"
-