view it in slide show mode.

wakecabbagepatchSoftware and s/w Development

Nov 18, 2013 (3 years and 6 months ago)

57 views

This presentation includes custom animations.


To view the animations, you must view the presentation in Slide Show mode

and activeX controls must be allowed.


If you have opened this lesson in PowerPoint, use the PowerPoint menus to
view it in slide show mode.


If you have opened this lesson in a browser and see a bar similar to that below,
click on the Slide Show icon

A notice similar to the one below may appear warning that ActiveX or
other scripts are disabled. Enable the controls for this website in order
to see the animations.

C++


This slide show introduces C++ & compares it to C.

Christine S. Wolfe

Ohio University Lancaster

2008
-
Aug
-
01

Vocabulary:

attribute

bool

cin

cout

encapsulation

iostream

method

namespace

object
oriented

private

public

string

w_char




C appeared in 1972. It was developed by Dennis Ritchie at Bell Labs.

In 1979, Bjarn Stroustrup at Bell Labs
became interested in adding classes to
C and overcoming some of the common
problems with C. His additions were
adopted in 1983 as C++. C++ was first
standardized in 1998.

C++ is a superset of C.

C++

C

The name:


BCPl (Basic Combined Programming Language)

…evolved into a language nicknamed “B”



Dennis Ritchie adopted parts of B into his new language.

Because it was a successor to B, he called it C.



Bjarne Stoustrup extended the C language to invent C++

He coined the name C++ to imitate the increment operator (++)


adapted from Dale. Weems. “Programming and Problem Solving with C++)

C++ supports (but does not require) object
-
oriented programming


The main objects are in the std namespace and the line


using namespace std;

appears after the last #include statement

C Fundamental Data Types


void


char


int


float


double

C++ Fundamental Data Types


void


char


int


float


double


bool


wchar_t

C++ includes two additional data types

Differences:

Standard I/O

C




#include <stdio.h>


printf


scanf

C++


#include <iostream>

#include <iomanip>


cout


cin

cout << “The answer is “ << Result <<endl;

printf(“The answer is %d
\
n”, Result);

cin >> Rate;

scanf(“
\
n%f”, &Rate)

cout

<<

setw
(8) <<
setprecision
(4) <<
my_float
;

printf(“%8.4f“, my_float);

C++ provides a robust string header that includes a string data type.

C++ Code


#include <iostream>

#include <string>

using namespace std;


int main ()

{


string FirstName;


FirstName = “Oscar”;


return 0;

}

C Code


#include <stdio.h>

#include <string.h>



int main ()

{


char FirstName[25];


strcpy(FirstName, “Oscar”);


return 0;

}

// my first program in C++

#include <iostream>


using namespace std;


int main ()

{


cout << "Hello World!";


return 0;

}

/* my first program in C */

#include <stdio.h>




int main ()

{


printf("Hello World!“);


return 0;

}

C vs C++ Comparison of the Hello World program

C and C++ use the same arithmetic operators:

+
-

* / %

C and C++ use the same logical operators:


! && ||

C and C++ use the same comparison operators:

== < <= > >= !=

C and C++ use the same assignment operators:

= *= /= %= +=
-
=

C and C++ use the same unary operators:


++
--

-

C vs C++ keyboard input

scanf(“
\
n %d”, &x);

#include <iostream>

#include <iomanip.h>

#include <stdio.h>


cin >> x;


In C, the programmer can define a new data type based on the fundamental types.

The new data type is called a structure and is declared using the struct keyword.

Inside the structure, attributes are defined based on existing data types.


struct TestScore


{



char Pnumber[8];



int TestNum;



float score;


};

Variables can now be declared as type TestScore.

variable names









data types









int Age;



float Salary;



TestScore StudentIn;

http://cplusplus.com

A class is an expanded concept of a data structure: instead of holding
only data, it can hold both data and functions.

class Cat

{


public:


int GetAge(void);


void SetAge(int age);


void Meow(void);


private:


int itsAge;

} ;

The concept that all the attributes and behaviors of an entity are bound
together in a single class is called
Encapsulation

C
++ expands the structure to include both the data (attributes) and
functions (methods).

Procedure oriented programming

the traditional programming that is based on algorithms or a logical
step
-
by
-
step process for solving a problem.

http://www.pcai.com/web/glossary/pcai_p_s_glossary.html

a class of programming languages and techniques based on
the concept of an “object” which is a data structure
encapsulated with a set of routines which operate on the data.
C++ and Java are object oriented programs

http://www.powerhousemuseum.com/hsc/lexicon/glossary.htm

Object oriented programming

When a language supports the use of classes/objects,

it is referred to as object
-
oriented.

attributes

Cat

itsAge

GetAge(void);

SetAge(int age);

Meow(void);

methods

class name

Classes are diagrammed as shown below.

object









class









int Age;


float


Salary;


TestScore StudentIn;


Cat Puff;

An object is an instantiation of a class. In terms of variables, a class would
be the type, and an object would be the variable.


class Cat

{


public:


int GetAge(void);


void SetAge(int age);


void Meow(void);


private:


int itsAge;

} ;

class Cat

{


public:


int GetAge(void);


void SetAge(int age);


void Meow(void);


private:


int itsAge;

} ;


void Cat::SetAge(int age)

{


itsAge = age;

}

Each method must be defined.

Methods are defined in the same manner as a subfunction.

The name of the method is justified by the prepending the class name + ::

method
prototype

method
definition

int main()

{


Cat Frisky;


int Years;


int YearCtr;


printf("
\
nPlease enter Frisky's age ");


fflush(stdin);


scanf("
\
n%d", &Years);


Frisky.SetAge(Years);


cout << "Frisky is a cat who is ";


cout << Frisky.GetAge() << " years old.
\
n";


for(YearCtr = 1; YearCtr <= Years; ++YearCtr)


{



Frisky.Meow();


}


printf("
\
n
\
n
\
n
\
n");


return 0;

}

class Cat

{


public:


int GetAge(void);


void SetAge(int age);


void Meow(void);


private:


int itsAge;

} ;

When calling a method,

or referring to an attribute of the class,

prepend the OBJECT name (not the class name)

and a period to the method or attribute.

Differences:

String class

C



#include <string.h>


…for C
-
strings


C++


#include <cstring>


…for C
-
strings


#include <string>


…for string class



A C
-
string is an array of chars terminated with NULL

A class string is an object and has methods that can be used to
manipulate the string.


By default, a literal such as “Ohio University” is a C
-
string.

Declaring strings

C
-
strings are declared and manipulated as in C


string class objects are declared as type string


string FirstName;

string Greeting = “Hello”;


A string declared as type string can...


use the assignment operator

FirstName = “Jane”;


use the + operator


Greeting = Greeting + “ “ + Firstname

(note, at least one of the operands must be a string class object)


use string methods


FirstName.substr etc


use comparison operators


== != < <= >= >

(watch out for upper/lower case)

C++


cin >>


>> (extraction operator)

extracts desired data value from the input stream

skips any leading whitespace characters


if data value is a char >> extracts exactly 1 character

if data value is an int >> extracts characters until first inappropriate character

if data value is a float >> extracts characters until first inappropriate character

if data value is a string >> extracts characters until first whitespace


Examples: (given: i & j are ints, x is float, ch is char, str is a string)

Statement


Data



Result


cin >> I;



32

cin >> i >> j;


4 60

cin >> i >> ch >> x; 25 A 16.9

cin >> i >> ch >> x;

25A16.9

cin >> i >> j >> x;


12 8

cin >> i >> x;


46 32.4 15

cin >> str;


Jane

cin >> str;


Jane Doe

C++


Drawbacks to the >> operator when inputting strings


1.
If string isn’t large enough, the >> operator will continue to store characters
into memory beyond the end of the array.


2.
The >> operator cannot be used to input strings with embedded whitespace.

Solution
cin.get
()

(assume
ch

is a char and
str

is a string and n is an int)


get()
does not

skip leading whitespace


cin.get
(
ch

);


reads a single character (even a whitespace)


cin.get
(
str
, n);


reads up to n
-
1 chars & appends ‘
\
0’




stops at newline, or n
-
1 chars




newline left in stream


cin.get
(
str
, n , delimiter)

reads up to n
-
1 chars & appends ‘
\
0’




stops at delimiter, or n
-
1 chars




delimiter left in stream


getline
(
cin
,
str
)


reads entire line up to ‘
\
n’




extracts newline from stream and stores