CS1061: C Programming Lecture 22: A Brief Lookahead at C++

Download Report

Transcript CS1061: C Programming Lecture 22: A Brief Lookahead at C++

CS1061: C Programming
Lecture 22: A Brief Look at C++
A. O’Riordan, 2004
Object Oriented Programming

C++ is an object-oriented programming language (OOPL) based on C.

Programmers define entities called objects and define communication between
the objects in the form of (member) function invocation.

Objects may consist of both program logic and data.

Programming consists of defining classes of these objects and their
interrelationships, i.e. behaviour.
C++ = C + Objects

C++ is based on C. C++ gets its name from the C increment operator ++.

C++ was an attempt to invent a programming language that supported "object
oriented" abstractions, yet remained compatible with the widely used C
language.

Thus programmers can reap the benefits of object orientation without having
to throw away their existing C programs.

C++ is nearly-but not exactly-a superset of ISO C.
The C++ Language

C++ was developed by Bjarne Stoustrup at AT&T in the 1980s, but only
became an ISO standard in 1998.

C++ is a problematic language. For example the pointer implementation is
considered too low-level. Also there is no automatic garbage collection, i.e.
you cannot automatically reclaim used memory when it becomes available. It
has complex semantics, and is hard to learn.

Modern languages such as Java and C# aim to combat and remedy many of
these disadvantages.


http://java.sun.com/
http://msdn.microsoft.com/vcsharp/
C versus C++
Many constructs in C and C++ are the same (or almost exactly the same)






Assignment Statement
Arithmetic
Looping – for, while, do..while
Decision Statements – if, switch
Arrays and Pointers
Logic and Relational Operaters
Other aspects of the language are quite different



Data Definition – struct versus class
Function Invocation – messages in C++
Input/Output
Input/Output
Input/output is also significantly different in C++ and is based on streams. Here is
the "HelloWorld" program.
#include<iostream>
using namespace std;
int main(){
// Display HelloWorld on the screen
cout << "HelloWorld" << endl;
return(0);
}
Example with Function Call
#include <iostream>
using namespace std;
void display_char(char);
int main(){
char my_c;
cin >> my_c;
display_char(my_c);
return 0;
}
void display_char(char c){
cout << "Character is " << c << endl;
return;
}
String example
Here is a simple example – need to include sting header file.
#include <string>
using namespace std;
int main(){
string salutation = "Hello\n";
cout << salutation;
return 0;
}
Reference Parameters
An alternative is pointers is available in C++. This is to use reference parameters:
#include<iostream>
void add_VAT(float);
int nain(){
add_VAT(priceItem);
cout << "Price plus VAT is " << priceItem << endl;
return 0;
}
void add_VAT(float &refPriceItem){
refPriceItem*= (1 + VATRate);
}
Class Concept

Objects consist of both data and functionality.

In C++ this equates to data members and member functions.

Classes in C++ are implemented as an extension of the struct mechanism in C.

A fundamental construct in object oriented programming is that of inheritance.
It is common when classifying things in the real world to order them into
hierarchical structures. We can likewise arrange C++ classes into hierarchies.
Encapsulation
In C++ we encapsulate the data and functions in a class. The elements of the class
are called members. Here is an example of a bank account class.
class DepositAccount{
public:
DepositAccount(int accnum);
void makeWithdrawal(int amt);
void makeLodgement(int amt);
void printStatement();
private:
int accountNumber;
float balance;
Data
};
Private
Public
Permissible operations
Member Functions

An example member function implementation is given here:
void DepositAccount::makeWithdrawal(int amt){
if (amt > balance)
cout << "You do not have enough in account" << endl;
else
balance -= amt;
}

And called as follows:
DepositAccount my_da;
my_da.makeWithdrawal(100);
More on C++
For treatment of the C++ language there are numerous excellent books. Here are
two:


The C++ primer, 3rd edition, Stanley B. Lippman, Addison-Wesley, 1998.
The C++ Programming Language, 3rd edition, Bjarne Stroustrup, AddisonWesley, 1997.