Giao Trinh BK Tech
Giao Trinh BK Tech
Giao Trinh BK Tech
C++
Programming
C Author: Thai Van Sang
Version: 1.1.2023
C/C++ Course (18 sessions)
❖ Session 1 : Overview, Memory layout and Stack frame of C/C++ .
❖ Session 2 : Investigate about Primary variables and Scope of variables (const, static, extern, volatile).
❖ Session 3 : Investigate about Derived variables (Function, Default function, Variadic function, Array).
❖ Session 4 : Investigate about Derived variables (Pointer , Reference, Pointer & Array).
❖ Session 5 : Practice exercise 1.
❖ Session 6 : Type casting (Dynamic casting & Static casting) in C/C++.
❖ Session 7 : Investigate about Macro define and User defined variables (struct / enum / union).
❖ Session 8 : Investigate about User defined variables (Class).
❖ Session 9 : Practice exercise 2.
❖ Session 10 : Investigate about OOP (Class / Object / Encapsulation).
❖ Session 11 : Investigate about OOP (Polymorphism / Inheritance).
❖ Session 12 : Investigate about OOP (Abstract class / Friend class / Friend function).
❖ Session 13 : Investigate about OOP (Function overloading / Function overriding / Operator overloading).
❖ Session 14 : Practice exercise 3.
❖ Session 15 : Loops, Decision Making (For, While, Do-while, Switch case, If- Else,..).
❖ Session 16 : Vector, List, Set, Map.
❖ Session 17 : Implement project (Design virtual chip).
❖ Session 18 : Practice final exam.
Course Goals
G-01 G-04
01 Know and present 04 Know and practice
about C/C++ concepts basic OOP exercises
G-02 G-05
02 Know and practice 05 Know and practice
basic C/C++ exercises advanced OOP exercises
G-03 G-06
03 Know and present 06 Introduce C/C++ language
about OOP concepts application in embedded
system
Assessment
Q&A
Research Testing
Judgment
Learn online
Pass/Fail
Unit Design Coding
Pratice
How to compile a C/C++ program
Lesson 1
How to write basic C/C++ program
Terminal:
sim.exe –Info class –Cmd 01
Lesson 1
Memory layout in C/C++
(Refer : https://www.geeksforgeeks.org/data-types-in-c/)
C/C++ Variables - Primary
Predict the output of source code
C/C++ Variables - Derived
Variables Derived
Const variable
Syntax - Function
va_arg(va_list ap, This one accesses the next variadic function argument.
type) *va_list ap* is the same as above i.e a pointer to argN
*type* indicates the data type the *va_list ap* should expect (double, float, int etc.)
va_end(va_list ap) This ends the traversal of the variadic function arguments.
va_list Holds the information needed by va_start, va_arg, va_end, and va_copy.
Array
HEAP
Allocate dynamic
STACK
Allocate static
Syntax - Array
Syntax - Array
Static allocate & Dynamic allocate
Pointer & Reference
Pointers : A pointer is a variable that holds the memory address of another variable. A pointer needs to
be dereferenced with the * operator to access the memory location it points to.
References : A reference variable is an alias, that is, another name for an already existing variable. A
reference, like a pointer, is also implemented by storing the address of an object.
No References Pointers
Null Value It does not have null value. It can have value assigned as null.
This variable is referenced by the method pass The pointer does it work by the method
Arguments
by value. known as pass by reference.
Pointer & Reference
01 Const Pointer
03 Reference pointer
Pointer & Reference
Pointer & Reference
0x11223344
0x000000AA
0x000000AA mVar
0x0000FFFC
0x0000FFFC mP1
0xF000F000
mP2
Address of variable Value of variable
Pointer & Reference
0x0000FFFF
0xAAAA0000
8
ptr
0x0000FFFF
i,ref
Pointer & Reference
Double Pointer
Pointer & Reference
Reference Pointer
Practice exercise 1
❖ 1. Write a C/C++ program to allocate a block of memory for an array.
Test Data and Expected Output :
Input the number of elements to be stored in the array :
+ 5 Input 5 elements in the array :
• Element 1 : 25
• Element 2 : 30
• Element 3 : 35
• Element 4 : 20
• Element 5 : 40
❑ Values entered in the array are : 25 30 35 20 40
❖ 2. Write a C/C++ program to reverse the elements of an array.
+ Array before reverse : 25 30 35 20 40
+ Array after revese : 40 20 35 30 25
❖ 3. Write a C/C++ program to sort the elements of an array.
+ Array before sort : 25 30 35 20 40
+ Array after sort : 20 25 30 35 40
❖ 4. Write a C/C++ program to perform a binary search in an array.
Refer : https://www.w3resource.com/c-programming-exercises/array/index.php
C/C++ Variables – User Defined
08 04
Function Polymorphism
overloading
07 06 05
Operator
Friend function Inheritance
overloading
Class / Object
Syntax
Class ClassName
Access Modifiers
Private
Protected
Public
Class / Object
Constructor
Constructors are special class members which are called by the compiler every time an object of that
class is instantiated. Constructors have the same name as the class and may be defined inside or outside
the class definition.
Destructor
Destructor is another special member function that is called by the compiler when the scope of the object
ends.
Encapsulation / Abstraction
Abstraction Encapsulation
Encapsulation / Abstraction
Inheritance
Inheritance
Inheritance
Polymorphism
Polymorphism
Function overloading
Friend function
Operator overloading
Operators that can be overloaded Examples
Binary Arithmetic +, -, *, /, %
Function call ()
Subscript []
Register
APB bus AXI bus 4096
bytes
Memory Memory
RAM Register & Memory
Initial value : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
R/W : R R R R R R R R R R R/W R/W R/W R/W R/W R/W
Bit : 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- - - - - - - - - - - INT - - - EN
Initial value : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
R/W : R R R R R R R R R R R R R R R R/W
Initial value : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
R/W : R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W
Bit : 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SRC
Initial value : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
R/W : R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W
Initial value : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
R/W : R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W
Bit : 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DST
Initial value : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
R/W : R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W
Check CR.EN == 1 ?
yes
no
Start operation
Dump message not CR.EN = 0
start operation
Received interrupt
FILE STRUCTURE
UT_Top
Register
Test Pattern
Bus
Ram Memory
Explanation :
Command Argument 1 Argument 2
[WRITE] Address register Write data