University of Tripoli Computer Engineering Department: C++ Review (Some Basic C++)
University of Tripoli Computer Engineering Department: C++ Review (Some Basic C++)
Software
A name given to a single program or set of programs. Two main kinds: (1) Application SW useful programs that a user might need, wordprocessors, spreadsheets, accounts programs etc Other : (2) System SW special programs that help the computer to do its job, operating systems UNIX, windows, network SW
The Computer
Computers are made up of switches
Great for holding data in Binary (1/0) format.
Output Devices
Monitor Printer
The Computer
But, how does it know what to output when it gets a particular input?
Programmers write instructions for the computer.
The computer uses a Central Processing Unit (CPU) to process input and produce output based on the instructions it has been given. Random Access Memory (RAM) is used to store information currently in use. Including
Instructions. Data from an Input Device waiting to be processed. Processed data waiting to be sent to an Output Device.
Types of Languages
Machine Language
110110010110 Nearly impossible to understand without reference books.
Assembly Language
ADD $1,$2,$3 #Sum $2, $3, store in $1 Better than machine language, but still difficult to use.
High-Level Language
a=b+c //Add b and c, store the sum in a The code is more human readable more intuitive. Each line of code may do the work of a few lines of code in assembly/machine language.
Programming Techniques
1. 2. 3. 4.
1- Unstructured programming
people start learning programming by writing small and simple programs consisting only of one main program. Here ``main program'' stands for a sequence of commands or statements which modify data which is global throughout the whole program.
2- procedural programming
With procedural programming you are able to combine returning sequences of statements into one single place. A procedure call is used to invoke the procedure. After the sequence is processed, flow of control proceeds right after the position where the call was made
3- modular programming
With modular programming procedures of a common functionality are grouped together into separate modules. A program therefore no longer consists of only one single part. It is now divided into several smaller parts which interact through procedure calls and which form the whole program.
4- object-oriented programming
It is a programming paradigm using "objects" data structures consisting of data fields and methods together with their interactions to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, messaging, modularity, polymorphism, and inheritance. Many modern programming languages now support OOP, at least as an option.
1. 2.
Compilers. Interpreters.
1- Conventional Compilers
A compiler is a special program that processes statements written in a particular programming language and turns them into machine language or "code" that a computer's processor uses. Syntax of the language (the grammatical structure) syntax error Integrated Development Environment (IDE)
2- Interpreters
A high-level programming language translator that translates and runs the program at the same time. It converts one program statement into machine language, executes it, and then proceeds to the next statement. Slower, But Easier to Test Interpreted programs run slower than their compiler. Whereas the compiler translates the entire program before it is run, interpreters translate a line at a time while the program is being run.
Interpreters vs Compilers
Compiled languages are translated into machine language ahead of time (right). Interpreted languages are translated at runtime (middle) translate the original source code. Java and Visual Basic (left) interpreters translate "bytecode,.
What is an Object?
Everything in the world is an object A Car, flower, a tree, an animal A student, a professor A desk, a chair, a classroom, a building A university, a city, a country The world, the universe A subject such as CS, IS, Math, History,
Person
Making Classes: Creating, extending or reusing abstract data types. Making Objects interact: Creating objects from abstract data types and defining their relationships.
C++ and C
C is a subset of C++. Advantages: Existing C libraries can be used, efficient code can be generated. But: C++ has the same caveats and problems as C (e.g. pointer arithmetic,). C++ can be used both as a low level and as a high level language.
Procedural Approach
bool MakeDeposit(int accountNum,float amount); float Withdraw(int accountNum,float amount);
struct Account { char *name; int accountNum; float balance; char accountType; };
Object Oriented
Data and operations are grouped together
Account
Withdraw Deposit Transfer
Encapsulation
Encapsulation is the process of combining data and functions into a single unit called class. Using the method of encapsulation, the programmer cannot directly access the data. Data is only accessible through the functions existing inside the class. Data encapsulation led to the important concept of data hiding. Data hiding is the implementation details of a class that are hidden from the user.
Data Encapsulation
class Account { public: float withdraw(); void deposit(float amount); private: float balance; );
Advantages of Encapsulation
Protection Consistency Allows change
Jodie
Daria
Jane
Brittany
Instantiation
An Object is instantiated from a Class
BankAccount myAccount; myAccount = new BankAccount;
Object
Own copy of data Active in running program Occupies memory Has the set of operations given in the class
Classification
Account Checking Account Savings Account
Value First
Select Access
First Interest
Inheritance
A class which is a subtype of a more general class is said to be inherited from it. The sub-class inherits the base class data members and member functions
Inheritance contd
A sub-class has all data members of its base-class plus its own A sub-class has all member functions of its base class (with changes) plus its own Inheritance is meant to implement subtyping (dont abuse it)
Summary
What is Object Oriented Programming? Object-oriented programming is a method of implementation in which programs are organized as cooperative collections of objects, each of which represents an instance of some class, and whose classes are all members of one or more hierarchy of classes united via inheritance relationships
Designing Classes
If you need a class for students, you should ask: What shall we call it? What are its attributes? What methods are needed by Student? Any other methods? In most cases, you declare both fields and functions You declare a field using a data type and an identifier You declare a function by writing its prototype, which serves as the interface to the function
Designing Classes
To instantiate an object is to declare or create it Student aSophomore; aSophomore.displayStudentData(); A function that uses your class is a class client
Use both the class name and the scope resolution operator (::) when you implement a class function