• Tidak ada hasil yang ditemukan

 Unlike C, C++ is an OOP language

N/A
N/A
Protected

Academic year: 2023

Membagikan " Unlike C, C++ is an OOP language "

Copied!
48
0
0

Teks penuh

(1)

CompSci 373 Tutorial

C++ Programming

(2)

C++ Language

 Is an extension of C -- so that pretty much any valid C program will compile and run on a C++ compiler.

 Unlike C, C++ is an OOP language

 Every program must have a main method, which serves as the entry point to the program

 There is no in-built memory management (clean up your

own mess!)

(3)

C++ Data Type Review

bool: Boolean in Java, indicating true or false

int, unsigned int, short: for integers .e.g. 8, 19, 2012

float, double: for floating point numbers .e.g. 7.2658

char: for a single letter .e.g. ‘a’ or ‘m’, .etc.

int myArray[10]: an array having 10 elements of type of integer

 myArray[0] = 19;

 *: a pointer, an array or a string (a string is considered as an array of letters)

 int* myArray = new int[10];

 myArray[0] = 19;

 char* strName = “Luffy”;

(4)

C++ Program Structure

External library to be used - iostream provides input/output

(5)

C++ Program Structure

Program Entry Point

(6)

C++ Program Structure

Display a message on the screen

Add newline character “\n” at the end of the message Use output stream (display),

instead of input stream

(7)

C++ Program Structure

Stop here, and wait for 1 char to be entered

from the keyboard.

Effectively pausing the screen until the user

presses any key

(8)

C++ Program Structure

Finish the program and exit

(9)

Classes in C++

 Each class is often defined using 2 separate files.

 Header file (.h) contains the definition of the class. Things like methods’ signature, instance variables etc.

 Class file (.cpp) contains the actual implementation of

those methods

(10)

Classes in C++

Header file: vector.h Class file: vector.cpp

(11)

Classes in C++

Header file: vector.h Class file: vector.cpp

class name

(12)

Classes in C++

Header file: vector.h Class file: vector.cpp

Whatever declared in this block will be marked as public

(13)

Classes in C++

Header file: vector.h Class file: vector.cpp

Constructor of this class

<Constructors have NO return type>

(14)

Classes in C++

Header file: vector.h Class file: vector.cpp

Class methods

(15)

Classes in C++

Header file: vector.h Class file: vector.cpp

Overloading operators

(16)

Classes in C++

Header file: vector.h Class file: vector.cpp

Whatever declared in this block will be marked as private

(17)

Classes in C++

Header file: vector.h Class file: vector.cpp

private instance variables

(18)

Classes in C++

Header file: vector.h Class file: vector.cpp

Tell the compiler that we will use what have been declared in this file

(19)

Classes in C++

Header file: vector.h Class file: vector.cpp

Implement the constructor

(20)

Classes in C++

Header file: vector.h Class file: vector.cpp

The following signature belongs to class Vector

Constructor’s signature

(21)

Classes in C++

Header file: vector.h Class file: vector.cpp

this refers to the current class we are in

(22)

Classes in C++

Header file: vector.h Class file: vector.cpp

Provide the implementation for length method

(23)

Classes in C++

Header file: vector.h Class file: vector.cpp

Provide the implementation for length and normalise

method

(24)

Classes in C++

Header file: vector.h Class file: vector.cpp

Provide the implementation for length and normalise

method

(25)

Classes in C++

Header file: vector.h Class file: vector.cpp

(26)

Classes in C++

(27)

Classes in C++

Indicate that we are going to use Vector class

(28)

Classes in C++

Create 2 vectors. The defined constructor is called

(29)

Classes in C++

Call method length of the vector

Call method normalise of the vector

(30)

Operator Overloading

Declare and implement a method that performs the

task

Goal: provide a mean to add 2 vectors

 Solution: implement a method that take 2 vectors and

perform addition

(31)

Operator Overloading

Work fine!

But isn’t it more intuitive if we could have it this way?

(32)

Operator Overloading

C++ allows us to define the behaviour for those operators such as +, -, ++,

==, !=, etc.

(33)

Operator Overloading

C++ allows us to define the behaviour for those operators such as +, -, ++,

==, !=, etc.

Operator signature

(34)

Operator Overloading

C++ allows us to define the behaviour for those operators such as +, -, ++,

==, !=, etc.

Define the behaviour for “+” symbol

(35)

Operator Overloading

C++ allows us to define the behaviour for those operators such as +, -, ++,

==, !=, etc.

(36)

Read inputs from Users

Read the first input (separated by whitespace) and store

in x

12 02 35 56 The Buffer

(37)

Read inputs from Users

Read the second input (separated by whitespace) and store

in y

(38)

Read inputs from Users

(39)

Assignment Skeleton Code Explained

Project for question 2, modify codes in here

matrixf: contains the definition of a matrix or a vector

matrixops: contains a number of operations we can

perform on a matrix or a vector .e.g: transpose, multiple,

dot, cross product, etc.

(40)

Assignment Skeleton Code Explained

Describe a matrix or a vector

Keeping the number of rows and columns of the matrix

(41)

Assignment Skeleton Code Explained

Describe a matrix or a vector

Keeping the number of rows and columns of the matrix

(42)

Create a vector with 3 rows and 1 column

(43)

Fill in values

(44)

Create a 4x4 matrix

(45)

Header file: matrixops.h Class file: matrixops.cpp

(46)

Header file: matrixops.h Class file: matrixops.cpp

(47)

Header file: matrixops.h Class file: matrixops.cpp

NOT YET IMPLEMENTED YOUR TASK

(48)

Online Tutorials

 http://www.cplusplus.com/doc/tutorial/

 C++ from Java

 http://cs.brown.edu/courses/cs123/docs/java_to_c.shtml

Referensi

Dokumen terkait

Furthermore, using such a static factory method mandates that the client refer to the returned object by its interface rather than by its implementation class, which is generally

Selain itu juga dalam bahasa C kita akan mengenal file header, biasa ditulis dengan ekstensi h(*.h), adalah file bantuan yang yang digunakan untuk menyimpan daftar-daftar fungsi

Classes provide data hiding, guaranteed initialization of data, implicit type conversion for user-defined types, dynamic typing, user-controlled memory management, and mechanisms

§24.2 The fundamental role of classes, class hierarchies, type checking, and programming itself §24.3 Uses of classes and class hierarchies, focussing on dependencies between

One base is a public abstract class providing the interface and the other is a protected concrete class providing implementation ‘‘details.’’ These roles are reflected in both

One imperfect but simple method of achieving consistency for declarations in different translation units is to # i in nc cl lu ud de e header files containing

membuat lingkungan compiler C++ hampir tidak dapat digunakan. Karena antarmuka programmer ke class yangdi-compile dilakukan melalui file header-nya, maka kode yang

MATERIALS AND METHOD This study used a qualitative descriptive approach in order to provide an overview of the implementation of Content and Language Integrated Learning which is