0% found this document useful (0 votes)
61 views

University of Tripoli Computer Engineering Department: C++ Review (Some Basic C++)

This document provides an overview of object-oriented programming concepts and C++. It discusses what objects and classes are, how they are related, and key OOP principles like encapsulation, inheritance, and polymorphism. It also reviews C++ features like data types, control structures, and differences between C++ and other languages like C and Java. The document is intended as an introduction to object-oriented design and modeling concepts using the C++ programming language.

Uploaded by

Amira
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

University of Tripoli Computer Engineering Department: C++ Review (Some Basic C++)

This document provides an overview of object-oriented programming concepts and C++. It discusses what objects and classes are, how they are related, and key OOP principles like encapsulation, inheritance, and polymorphism. It also reviews C++ features like data types, control structures, and differences between C++ and other languages like C and Java. The document is intended as an introduction to object-oriented design and modeling concepts using the C++ programming language.

Uploaded by

Amira
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

University of Tripoli Computer Engineering Department

SoC Design Modelling with SystemC EC580 Spring 2012

C++ Review (some basic C++)

Take a step back


What is a computer?
A device that performs calculations.

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

Take a step back - cont.

Application and System Software

The Computer
Computers are made up of switches
Great for holding data in Binary (1/0) format.

Computers take input and produce output


Input Devices
Keyboard Scanner

Output Devices
Monitor Printer

Input/Output (I/O) Devices


Hard Disk Network Connection

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.

Unstructured programming, procedural programming, modular programming and object-oriented programming.

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.

How to Execute a Program?

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)

Conventional Compilers cont.

Compiling and running conventional programs

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,.

Object Oriented Programming

What are we doing today?


Introduction of:
Objects Basic Terminology Review of C++

Why do we care about object oriented programming?


Modularity - large software projects can be split up in smaller pieces. Reusability - Programs can be assembled from pre-written software components. Extensibility - New software components can be written or developed from existing ones.

What is an Object?

An object is like a black box. The internal details are hidden.

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,

The two parts of an object


Object = Data + Methods or to say the same differently: An object has the responsibility to know and the responsibility to do. Methods
Data

The two parts of an object


Data: name, date_of_birth, sex, etc.

Methods: getName(), getDOB(), getSex(). Etc.

Person

Thinking of creating Objects


Induction (Abstract)
From specialization to generalization
From different cars to create the word car
Car

The development of a system is caused by the interactions

Our University is developed by the interactions among:


students professors staffs ...

The two steps of Object Oriented Programming

Making Classes: Creating, extending or reusing abstract data types. Making Objects interact: Creating objects from abstract data types and defining their relationships.

Historical Notes of C++


C++ owes most to C. First versions of C++ in 1980 under the name C with classes. Since 1983 the name C++ is used. 1990: ANSI/ISO 9899 defines a standard for C 1998: ISO/IEC 14882 specifies the standard for C++

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.

C++ and Java


Java is a full object oriented language, all code has to go into classes. C++ - in contrast - is a hybrid language, capable both of functional and object oriented programming. So, C++ is more powerful but also more difficult to handle than Java.

EX: Bank Account


Problem Description: customers are allowed to have different types of bank accounts, deposit money, withdraw money and transfer money between accounts

Procedural Approach
bool MakeDeposit(int accountNum,float amount); float Withdraw(int accountNum,float amount);

struct Account { char *name; int accountNum; float balance; char accountType; };

Procedural Approach contd


Focus is on procedures All data is shared: no protection More difficult to modify Hard to manage complexity

Procedural vs. Object-Oriented


Procedural Object Oriented

Withdraw, deposit, transfer

Customer, money, account

Mapping the world to software


Objects in the problem domain are mapped to objects in software

0110100 011101 10011 1110101 11010 10101 010101 11101

Object Oriented
Data and operations are grouped together

Account
Withdraw Deposit Transfer

Interface: Set of available operations

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

Objects and Classes


Classes reflect concepts, objects reflect instances that embody those concepts. object class
girl

Jodie

Daria

Jane

Brittany

Objects and Classes contd


A class captures the common properties of the objects instantiated from it A class characterizes the common behavior of all the objects that are its instances

Objects and Classes contd


Class BankAccount Balance InterestYTD Owner Account_number
Operations MakeDesposit Transfer WithDraw GetBalance

Balance 500 InterestYTD Owner Account_number

Balance 10,000 InterestYTD Owner Account_number

Objects as instances of Classes


The world conceptually consists of objects Many objects can be said to be of the same type or class
My bank account, your bank account, Bill Gates bank account

We call the object type a class

Instantiation
An Object is instantiated from a Class
BankAccount myAccount; myAccount = new BankAccount;

Objects and Classes


Class
Visible in source code The code is not duplicated

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

Implementing Class Functions


When you construct a class, you create two parts:
Declaration section: contains the class name, variables (attributes), and function prototypes Implementation section: contains the functions

Use both the class name and the scope resolution operator (::) when you implement a class function

Implementing Class Functions (continued)

Using Public Functions to Alter Private Data

Using Private Functions and Public Data

Considering Scope when Defining Member Functions

Considering Scope when Defining Member Functions (continued)

You might also like