0% found this document useful (0 votes)
31 views45 pages

DSA Ch1 Introduction

This document introduces basic concepts related to data structures and algorithms. It discusses data, data types, data structures, abstract data types, algorithms, and pseudocode. It also outlines the chapters to come, which will cover revision of data structures, classes, pointers, arrays, pointers to structures, and pointers to classes. The sources of materials are acknowledged.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views45 pages

DSA Ch1 Introduction

This document introduces basic concepts related to data structures and algorithms. It discusses data, data types, data structures, abstract data types, algorithms, and pseudocode. It also outlines the chapters to come, which will cover revision of data structures, classes, pointers, arrays, pointers to structures, and pointers to classes. The sources of materials are acknowledged.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Introduction

Dr. NGUYEN Ho
Man Rang

Chapter 1
Introduction Basic concepts
Data
Data type
Data structure
Data Structures and Algorithms - CO2003 Abstract data type
Algorithm
Pseudocode

Revision
Data structures

Dr. NGUYEN Ho Man Rang Classes


Pointers

Faculty of Computer Science and Engineering Arrays


Pointers to structures
Ho Chi Minh University of Technology, VNU-HCM Pointers to classes

1.1
Introduction
Overview
Dr. NGUYEN Ho
Man Rang

1 Basic concepts
Data
Data type
Data structure
Abstract data type Basic concepts
Data
Algorithm Data type
Data structure
Pseudocode Abstract data type
Algorithm
Pseudocode

2 Revision Revision
Data structures Data structures
Classes

Classes Pointers
Arrays

Pointers Pointers to structures


Pointers to classes
Arrays
Pointers to structures
Pointers to classes

1.2
Introduction
Sources of Materials
Dr. NGUYEN Ho
Man Rang

1 We would like to thank Dr. The-Nhan LUONG, a


former instructor of our Department, for the composing
of this document. Basic concepts
Data

2 This document also uses figure, sentences and demo Data type
Data structure

source code from the following sources: Abstract data type


Algorithm

• The old presentation for course Data Structures and Pseudocode

Revision
Algorithms edited by other members in our Department Data structures
• Book entitled Data Structures - A Pseudocode Classes
Pointers
Approach with C++ (first edition, 2001) written by Arrays

Richard F. Gilberg and Behrouz A. Forouzan Pointers to structures


Pointers to classes

1.3
Introduction

Dr. NGUYEN Ho
Man Rang

Basic concepts

Basic concepts
Data
Data type
Data structure
Abstract data type
Algorithm
Pseudocode

Revision
Data structures
Classes
Pointers
Arrays
Pointers to structures
Pointers to classes

1.4
Introduction
What is Data?
Dr. NGUYEN Ho
Man Rang

Basic concepts
Data
Data type
Data structure
Abstract data type
Algorithm
Pseudocode

Revision
Data structures
Classes
Pointers
Arrays
Pointers to structures
Pointers to classes

(Source: datorama.com) 1.5


Introduction
What is Data?
Dr. NGUYEN Ho
Man Rang
Data
Data is information that has been translated into a form
that is more convenient to calculate, analyze.

Example Basic concepts


Data

• Numbers, words, measurements, observations or Data type


Data structure

descriptions of things. Abstract data type


Algorithm
Pseudocode

Revision
Data structures
Classes
Pointers
• Qualitative data: descriptive information, Arrays
Pointers to structures
• Quantitative data: numerical information (numbers). Pointers to classes

• Discrete data can only take certain values (like whole


numbers)
• Continuous data can take any value (within a range)

1.6
Introduction
Data type
Dr. NGUYEN Ho
Man Rang

Class of data objects that have the same properties.


Data type

1 A set of values
Basic concepts
2 A set of operations on values Data
Data type
Data structure
Abstract data type

Example Algorithm
Pseudocode

Type Values Operations Revision


Data structures
integer −∞, ..., −2, −1, ∗, +, −, %, /, Classes
Pointers
0, 1, 2, ..., ∞ ++, −−, ... Arrays
Pointers to structures
floating point −∞, ..., 0.0, ..., ∞ ∗, +, −, /, ... Pointers to classes

character \0, ..., ‘A’, ‘B’, ..., <, >, ...


‘a’, ‘b’, ..., ∼

1.7
Introduction
Data structure
Dr. NGUYEN Ho
Man Rang

What is a data structure?

1 A combination of elements in which each is either a


data type or another data structure
Basic concepts
2 A set of associations or relationships (structure) that Data

holds the data together Data type


Data structure
Abstract data type
Algorithm
Pseudocode

Example Revision
Data structures
An array is a number of elements of the same type in a Classes
Pointers
specific order. Arrays
Pointers to structures
Pointers to classes

1 2 3 5 8 13 21 34

1.8
Introduction
Abstract data type
Dr. NGUYEN Ho
Man Rang

The concept of abstraction:

• Users know what a data type can do.


• How it is done is hidden.
Basic concepts
Data
Data type

Definition Data structure


Abstract data type

An abstract data type is a data declaration packaged Algorithm


Pseudocode

together with the operations that are meaningful for the Revision
data type. Data structures
Classes
Pointers
Arrays

1 Declaration of data Pointers to structures


Pointers to classes

2 Declaration of operations
3 Encapsulation of data and operations

1.9
Introduction
Abstract data type
Dr. NGUYEN Ho
Man Rang

Basic concepts
Data
Data type
Data structure
Abstract data type
Algorithm
Pseudocode

Revision
Data structures
Classes
Pointers
Arrays
Pointers to structures
Pointers to classes

Figure: Abstract data type model (source: Slideshare)

1.10
Introduction
Example: List
Dr. NGUYEN Ho
Man Rang

Interface

• Data: sequence of elements of a particular


data type Basic concepts

• Operations: accessing, insertion, deletion Data


Data type
Data structure
Abstract data type
Algorithm
Pseudocode

Revision
Data structures
Classes
Implementation Pointers
Arrays
Pointers to structures

• Array Pointers to classes

• Linked list

1.11
Introduction
Algorithm
Dr. NGUYEN Ho
Man Rang

What is an algorithm?

The logical steps to solve a problem. Basic concepts


Data
Data type
Data structure
Abstract data type
Algorithm
Pseudocode

What is a program? Revision


Data structures

Program = Data structures + Algorithms Classes


Pointers
Arrays
(Niklaus Wirth) Pointers to structures
Pointers to classes

1.12
Introduction
Pseudocode
Dr. NGUYEN Ho
Man Rang

• The most common tool to define


algorithms
Basic concepts

• English-like representation of the Data


Data type
Data structure

algorithm logic Abstract data type


Algorithm
Pseudocode

Revision

• Pseudocode = English + code Data structures


Classes
Pointers
Arrays
Pointers to structures
Pointers to classes

instructions using basic


relaxed syntax being easy control structures (sequen-
to read tial, conditional, iterative)

1.13
Introduction
Pseudocode
Dr. NGUYEN Ho
Man Rang
Algorithm Header

• Name
• Parameters and their types
• Purpose: what the algorithm does
• Precondition: precursor requirements for the parameters Basic concepts
Data

• Postcondition: taken action and status of the parameters Data type


Data structure

• Return condition: returned value Abstract data type


Algorithm
Pseudocode

Revision
Algorithm Body Data structures
Classes
Pointers
• Statements Arrays
Pointers to structures
• Statement numbers: decimal notation to express levels Pointers to classes

• Variables: important data


• Algorithm analysis: comments to explain salient points
• Statement constructs: sequence, selection, iteration
1.14
Introduction
Pseudocode: Example
Dr. NGUYEN Ho
Man Rang

Algorithm average
Pre nothing
Post the average of the input numbers is printed
1 i=0
Basic concepts
2 sum = 0 Data
Data type
3 while all numbers not read do Data structure

4 i=i+1 Abstract data type


Algorithm

5 read number Pseudocode

Revision
6 sum = sum + number Data structures

7 end Classes
Pointers

8 average = sum / i Arrays


Pointers to structures

9 print average Pointers to classes

10 End average
Algorithm 1: How to calculate the average

1.15
Introduction

Dr. NGUYEN Ho
Man Rang

Basic concepts
Data

Revision Data type


Data structure
Abstract data type
Algorithm
Pseudocode

Revision
Data structures
Classes
Pointers
Arrays
Pointers to structures
Pointers to classes

1.16
Introduction
Data structures
Dr. NGUYEN Ho
Man Rang

Data structures can be declared in C++ using the following


syntax:
struct [ type_name ] {
member_type1 member_name1 ;
member_type2 member_name2 ; Basic concepts
member_type3 member_name3 ; Data
Data type
... Data structure

} [ object_names ]; Abstract data type


Algorithm
Pseudocode

Revision
• Where type_name is a name for the structure type, Data structures
Classes
object_names can be a set of valid identifiers for Pointers

objects that have the type of this structure. Arrays


Pointers to structures

• Within braces { }, there is a list with the data Pointers to classes

members, each one is specified with a type and a valid


identifier as its name.
• struct requires either a type_name or at least one
name in object_names, but not necessarily both. 1.17
Introduction
Data structures
Dr. NGUYEN Ho
Man Rang

Example

struct car_t {
int year ;
string brand ;
Basic concepts
}; Data
Data type
Data structure
car_t toyota ; Abstract data type
car_t mercedes , bmw ; Algorithm
Pseudocode

Revision
Data structures
Example Classes
Pointers
Arrays
struct { Pointers to structures

int year ; Pointers to classes

string brand ;
} toyota , mercedes , bmw ;

1.18
Introduction
Data structures
Dr. NGUYEN Ho
Man Rang

A member of an object can be accessed directly by a dot (.)


inserted between the object name and the member name.
Example

toyota . year Basic concepts


Data
toyota . brand Data type
mercedes . year Data structure
Abstract data type
mercedes . brand Algorithm
bmw . year Pseudocode

bmw . brand Revision


Data structures
Classes

• toyota.year, mercedes.year, and bmw.year are of Pointers


Arrays

type int. Pointers to structures


Pointers to classes

• toyota.brand, mercedes.brand, and bmw.brand are


of type string.

1.19
Introduction
Data structures
Dr. NGUYEN Ho
Man Rang
Example

// example about structures


# include < iostream >

using namespace std ;


Basic concepts
Data

struct car_t { Data type


Data structure
int year ; Abstract data type

string brand ; Algorithm


Pseudocode
} mycar ;
Revision
Data structures
int main () { Classes
Pointers
mycar . brand = " Audi " ; Arrays
mycar . year = 2011; Pointers to structures
Pointers to classes
cout << " My ␣ favorite ␣ car ␣ is : " << endl ;
cout << mycar . brand << " ␣ ( " << mycar . year << " ) " ;
return 0;
}

1.20
Introduction
Data structures
Dr. NGUYEN Ho
Example Man Rang

# include < iostream >


using namespace std ;

struct car_t {
int year ; Basic concepts
string brand ; Data
Data type
} mycar ; Data structure
void printcar ( car_t ); Abstract data type
Algorithm
Pseudocode
int main () { Revision
mycar . brand = " Audi " ; Data structures

mycar . year = 2011; Classes


Pointers
printcar ( mycar ); Arrays

return 0; Pointers to structures


Pointers to classes
}
void printcar ( car_t c ) {
cout << " My ␣ favorite ␣ car ␣ is : " << endl ;
cout << c . brand << " ␣ ( " << c . year << " ) " ;
}
1.21
Introduction
Data structures
Dr. NGUYEN Ho
Man Rang

Exercise
Basic concepts
Data
• Define a data structure student_t containing a Data type
Data structure
student’s name, firstname and age. Abstract data type
Algorithm
• Write a code in C++ to take input your data and Pseudocode

display it. Revision


Data structures
Classes
Pointers
Arrays
Pointers to structures
Pointers to classes

1.22
Introduction
Data structures
Dr. NGUYEN Ho
Man Rang
Exercise
#i n c l u d e <i o s t r e a m >
#i n c l u d e <s s t r e a m >
u s i n g namespace s t d ;
s t r u c t student_t {
s t r i n g name ;
string firstname ;
i n t a ge ; Basic concepts
}; Data
void i n f o s t u d e n t ( student_t ) ; Data type
Data structure
i n t main ( ) {
Abstract data type
student_t sv ;
Algorithm
string str ;
Pseudocode
c o u t << " E n t e r ␣ y o u r ␣name : ␣ " ;
g e t l i n e ( c i n , s v . name ) ; Revision
c o u t << " E n t e r ␣ y o u r ␣ f i r s t n a m e : ␣ " ; Data structures
g e t l i n e ( cin , sv . firstname ) ;
Classes
c o u t << " E n t e r ␣ y o u r ␣ age : ␣ " ;
Pointers
g e t l i n e ( cin , s t r ) ;
Arrays
s t r i n g s t r e a m ( s t r ) >> s v . age ;
Pointers to structures
i n f o s t u d e n t ( sv ) ;
Pointers to classes
return 0;
}
void i n f o s t u d e n t ( student_t s ) {
c o u t << "My␣name␣ i s ␣ " << s . name << " ␣ " << s . f i r s t n a m e << e n d l ;
c o u t << " I ␣am␣ " << s . age << " ␣ y e a r s ␣ o l d . " << e n d l ;
}

1.23
Introduction
Classes
Dr. NGUYEN Ho
Man Rang

Classes are defined using keyword class, with the following


syntax:
class class_name {
access _sp ec if ier _1 : member1 ; Basic concepts
Data
access _sp ec if ier _2 : member2 ; Data type
... Data structure
Abstract data type
} object_names ; Algorithm
Pseudocode

Revision
• Where class_name is a valid identifier for the class, Data structures
Classes

object_names is an optional list of names for objects Pointers


Arrays

of this class. Pointers to structures


Pointers to classes

• The body of the declaration can contain members,


which can either be data or function declarations, and
optionally access_specifiers.

1.24
Introduction
Classes
Dr. NGUYEN Ho
Man Rang

Example
Basic concepts
Data
class Rectangle {
Data type
int width , height ; Data structure
Abstract data type
public :
Algorithm
void set_values ( int , int ); Pseudocode

int area ( void ); Revision


} rect ; Data structures
Classes
Pointers
Arrays
Pointers to structures
Pointers to classes

1.25
Introduction
Classes
Dr. NGUYEN Ho
Man Rang
Example
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
class Rectangle {
i n t width , h e i g h t ;
public :
void set_values ( int , int ) ;
int area ( void ) ; Basic concepts
}; Data
Data type

void Rectangle : : set_values ( int x , int y ) { Data structure

width = x ; Abstract data type


height = y ; Algorithm
} Pseudocode

int Rectangle : : area () { Revision


return width∗height ; Data structures

} Classes
Pointers
i n t main ( ) { Arrays
Rectangle rectA , rectB ; Pointers to structures
rectA . set_values (3 ,4); Pointers to classes
rectB . set_values (5 ,6);
c o u t << " r e c t A ␣ a r e a : ␣ " << r e c t A . a r e a ( ) << e n d l ;
c o u t << " r e c t B ␣ a r e a : ␣ " << r e c t B . a r e a ( ) << e n d l ;
return 0;
}

1.26
Introduction
Classes
Dr. NGUYEN Ho
Man Rang
Constructors

• Automatically called whenever a new object of a class is


created.
• Initializing member variables or allocate storage of the
object. Basic concepts
Data

• Declared with a name that matches the class name and Data type
Data structure

without any return type; not even void. Abstract data type
Algorithm
Pseudocode

Revision
Example Data structures
Classes
Pointers

class Rectangle { Arrays


Pointers to structures
int width , height ; Pointers to classes

public :
Rectangle ( int , int );
int area ( void );
};

1.27
Introduction
Classes
Dr. NGUYEN Ho
Man Rang

Example
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
class Rectangle {
i n t width , h e i g h t ;
public :
Rectangle ( int , int ) ; Basic concepts
int area ( void ) ; Data
}; Data type
Data structure
Rectangle : : Rectangle ( int x , int y ) {
Abstract data type
width = x ;
Algorithm
height = y ;
Pseudocode
}
Revision
int Rectangle : : area () {
Data structures
return width∗height ;
Classes
}
Pointers
Arrays
i n t main ( ) {
Pointers to structures
Rectangle rectA (3 ,4);
Pointers to classes
Rectangle rectB (5 ,6);
c o u t << " r e c t A ␣ a r e a : ␣ " << r e c t A . a r e a ( ) << e n d l ;
c o u t << " r e c t B ␣ a r e a : ␣ " << r e c t B . a r e a ( ) << e n d l ;
return 0;
}

1.28
Introduction
Classes
Dr. NGUYEN Ho
Initialization Man Rang

• Member initialization:
class Rectangle {
int width ;
const i n t h e i g h t ; Basic concepts
public : Data
Data type
Rectangle ( int , int ) ; Data structure

... Abstract data type


Algorithm

}; Pseudocode

Rectangle ( int x , int y ) : height ( y ) { Revision


Data structures
width = x ; Classes

} Pointers
Arrays
Pointers to structures
Pointers to classes
i n t main ( ) {
Rectangle rectA (3 ,4);
...
}

1.29
Introduction
Pointers
Dr. NGUYEN Ho
Man Rang
Definition
A pointer is a variable whose value is the address of another
variable, i.e., direct address of the memory location.

Address-of operator (&) Basic concepts


Data
The address of a variable can be obtained by preceding the Data type

name of a variable with an ampersand sign (&), known as Data structure


Abstract data type

address-of operator. For example: Algorithm


Pseudocode

p = & value ; Revision


Data structures
Classes
Pointers
Arrays
Dereference operator (*) Pointers to structures
Pointers to classes

To access the variable pointed to by a pointer, we precede


the pointer name with the dereference operator (*).
value = * p ;

1.30
Introduction
Pointers
Dr. NGUYEN Ho
Man Rang

p
1000
Basic concepts
Data
Data type
value Data structure
Abstract data type

93 Algorithm
Pseudocode

999 1000 1001 Revision


Data structures
Classes
Pointers

p = & value ; Arrays


Pointers to structures
value = * p ; Pointers to classes

1.31
Introduction
Pointers
Dr. NGUYEN Ho
Man Rang
Example

int main ()
{
int v1 = 5 , v2 = 15;
int * p1 , * p2 ;
p1 = & v1 ; Basic concepts
Data
p2 = & v2 ; Data type

* p1 = 10; Data structure


Abstract data type
* p2 = * p1 ; Algorithm

p1 = p2 ; Pseudocode

* p1 = 20; Revision
Data structures
cout << " v1 ␣ = ␣ " << v1 << ’\ n ’; Classes
cout << " v2 ␣ = ␣ " << v2 << ’\ n ’; Pointers
Arrays
return 0; Pointers to structures
} Pointers to classes

Exercise
What is the output?
1.32
Introduction
Pointers
Dr. NGUYEN Ho
Man Rang

Exercise
i n t main ( )
{
i n t v1 = 5 , v2 = 1 5 ;
i n t ∗ p1 , ∗ p2 ;
p1 = &v1 ; // p1 = a d d r e s s o f v1 , p1 p o i n t s t o v1 Basic concepts
p2 = &v2 ; // p2 = a d d r e s s o f v2 , p2 p o i n t s t o v2 Data
∗p1 = 1 0 ; // v a l u e p o i n t e d t o by p1 = 1 0 , v1 = 10 Data type
∗p2 = ∗p1 ; // v a l u e p o i n t e d t o by p2 = v a l u e p o i n t e d by p1 , v2 = 10 Data structure
p1 = p2 ; // v a l u e o f p o i n t e r i s c o p i e d , p1 p o i n t s t o v2
Abstract data type
∗p1 = 2 0 ; // v a l u e p o i n t e d by p1 = 2 0 , v2 = 20
Algorithm
c o u t << " v1 ␣=␣ " << v1 << ’ \n ’ ;
Pseudocode
c o u t << " v2 ␣=␣ " << v2 << ’ \n ’ ;
return 0; Revision
} Data structures
Classes
Pointers
Arrays
Output Pointers to structures
Pointers to classes

v1 = 10
v2 = 20

1.33
Introduction
Arrays
Dr. NGUYEN Ho
Man Rang

Definition
An array is a series of elements of the same type placed in
contiguous memory locations that can be individually
referenced by a unique identifier with an index. Basic concepts
Data
type var_name [ number_of_elements ]; Data type
Data structure
Abstract data type
Algorithm

Example Pseudocode

Revision
Data structures
int num [8]; Classes
Pointers

0 1 2 3 4 5 6 7 Arrays
Pointers to structures
Pointers to classes

num

1.34
Introduction
Arrays
Dr. NGUYEN Ho
Initializing arrays Man Rang

int num [8];

int num [8] = { };

int num [8] = { 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 }; Basic concepts


Data
Data type
int num [8] = { 1 , 2 , 3 , 5 , 8 }; Data structure
Abstract data type
Algorithm
int num [] = { 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 }; Pseudocode

Revision
int num [] { 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 }; Data structures
Classes
Pointers
Arrays
Exercise Pointers to structures
Pointers to classes

For each declaration of num, what is the output?


for ( int i =0; i <8; i ++) {
cout << num [ i ] << endl ;
}
1.35
Introduction
Pointers and arrays
Dr. NGUYEN Ho
Man Rang

The concept of arrays is related to that of pointers. Arrays


work very much like pointers to their first elements, and,
Basic concepts
actually, an array can always be implicitly converted to the Data
Data type
pointer of the proper type. Data structure
Abstract data type
Algorithm
For example, consider these two declarations: Pseudocode

Revision
int myarray [10]; Data structures
int * mypointer ; Classes
Pointers

The following assignment operation would be valid: Arrays


Pointers to structures
Pointers to classes
mypointer = myarray ;

1.36
Introduction
Pointers and arrays
Dr. NGUYEN Ho
Example Man Rang

# include < iostream >


using namespace std ;
int main ()
{
int num [5]; Basic concepts
int * p ; Data
Data type
p = num ; * p = 1; Data structure

p ++; * p = 2; Abstract data type


Algorithm
p = & num [2]; * p = 3; Pseudocode

p = num + 3; * p = 5; Revision
p = num ; *( p +4) = 8; Data structures
Classes
for ( int n =0; n <5; n ++) Pointers
cout << num [ n ] << " ,␣ " ; Arrays
Pointers to structures
return 0; Pointers to classes
}

Exercise
What is the output? Explain.
1.37
Introduction
Pointers to structures
Dr. NGUYEN Ho
Man Rang
Structures can be pointed to by its own type of pointers:
struct car_t {
string brand ;
int year ;
};
Basic concepts
Data
car_t mycar ; Data type
car_t * pcar ; Data structure
Abstract data type
Algorithm

• mycar is an object of structure type car_t. Pseudocode

Revision
• pcar is a pointer to point to an object of structure type Data structures
Classes
car_t. Pointers
Arrays

The following code is valid: Pointers to structures


Pointers to classes

pcar = & mycar ;

The value of the pointer pcar would be assigned the address


of object mycar.
1.38
Introduction
Pointers to structures
Dr. NGUYEN Ho
Man Rang
arrow operator (->)
The arrow operator (->) is a dereference operator that is
used exclusively with pointers to objects that have members.
This operator serves to access the member of an object
directly from its address. Basic concepts
Data
pcar - > year Data type
Data structure
Abstract data type

Difference: Algorithm
Pseudocode

• Two expressions pcar->year and (*pcar).year are Revision


Data structures
equivalent, and both access the member year of the Classes
Pointers
data structure pointed by a pointer called pcar. Arrays
Pointers to structures
• Two expressions *mycar.year or *(mycar.year) are Pointers to classes

equivalent. This would access the value pointed by a


hypothetical pointer member called year of the
structure object mycar (which is not the case, since year
is not a pointer type).
1.39
Introduction
Pointers to structures
Dr. NGUYEN Ho
Man Rang

Combinations of the operators for pointers and for structure


members:
Basic concepts
Data
Expression Equivalent What is evaluated Data type
Data structure

a.b Member b of object a Abstract data type


Algorithm
pa->b (*pa).b Member b of object pointed to Pseudocode

by pa Revision
Data structures
*a.b *(a.b) Value pointed to by member b Classes
Pointers
of object a Arrays
Pointers to structures
Pointers to classes

1.40
Introduction
Pointers to structures
Dr. NGUYEN Ho
Man Rang

Exercise
Basic concepts
Data
• Define a data structure student_t containing a Data type
Data structure
student’s name, firstname and age. Abstract data type
Algorithm
• Write a code in C++ using pointers to structures to Pseudocode

take input your data and display it. Revision


Data structures
Classes
Pointers
Arrays
Pointers to structures
Pointers to classes

1.41
Introduction
Pointers to structures
Dr. NGUYEN Ho
Man Rang
Exercise
#i n c l u d e <i o s t r e a m >
#i n c l u d e <s s t r e a m >
u s i n g namespace s t d ;
s t r u c t student_t {
s t r i n g name ;
string firstname ;
i n t a ge ;
Basic concepts
};
Data
void i n f o s t u d e n t ( student_t ∗);
Data type

i n t main ( ) { Data structure

student_t sv ; Abstract data type

s t u d e n t _ t ∗ p s v = &s v ; Algorithm
string str ; Pseudocode
c o u t << " E n t e r ␣ y o u r ␣name : ␣ " ;
g e t l i n e ( c i n , psv−>name ) ; Revision
c o u t << " E n t e r ␣ y o u r ␣ f i r s t n a m e : ␣ " ; Data structures

g e t l i n e ( c i n , psv−>f i r s t n a m e ) ; Classes

c o u t << " E n t e r ␣ y o u r ␣ age : ␣ " ; Pointers


g e t l i n e ( cin , s t r ) ; Arrays
s t r i n g s t r e a m ( s t r ) >> psv−>age ; Pointers to structures
i n f o s t u d e n t ( psv ) ; Pointers to classes
return 0;
}
void i n f o s t u d e n t ( student_t ∗s ) {
c o u t << "My␣name␣ i s ␣ " << s−>name << " ␣ " << s−>f i r s t n a m e << e n d l ;
c o u t << " I ␣am␣ " << s−>age << " ␣ y e a r s ␣ o l d . " << e n d l ;
}

1.42
Introduction
Pointers to structures
Dr. NGUYEN Ho
Man Rang

Structures can also be nested in such a way that an element


of a structure is itself another structure:
Example

struct car_t { Basic concepts


Data
string brand ; Data type
int year ; Data structure
Abstract data type
}; Algorithm
Pseudocode

struct friends_t { Revision


string name ; Data structures
Classes
string email ; Pointers

car_t favorite_car ; Arrays


Pointers to structures
} bobby , tommy ; Pointers to classes

friends_t * pfriend = & bobby ;

1.43
Introduction
Pointers to structures
Dr. NGUYEN Ho
Man Rang

After the previous declarations, all of the following


expressions would be valid:
Example
Basic concepts
Data
tommy . name
Data type
tommy . email Data structure
Abstract data type
tommy . favorite_car . brand
Algorithm
tommy . favorite_car . year Pseudocode

Revision
Data structures
Classes
bobby . name | pfriend - > name Pointers

bobby . email | pfriend - > email Arrays


Pointers to structures
bobby . favorite_car . brand | pfriend - > favorite_car . brand
Pointers to classes

bobby . favorite_car . year | pfriend - > favorite_car . year

1.44
Introduction
Pointers to classes
Dr. NGUYEN Ho
Man Rang
Example

#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
class Rectangle {
i n t width , h e i g h t ;
public :
Basic concepts
R e c t a n g l e ( i n t x , i n t y ) : w i d t h ( x ) , h e i g h t ( y ) {}
Data
i n t area ( void ) { return width ∗ height ; } Data type
}; Data structure
Abstract data type
Algorithm
i n t main ( ) { Pseudocode
Rectangle rectA (3 , 4);
Revision
R e c t a n g l e ∗ r e c t B = &r e c t A ;
Data structures
R e c t a n g l e ∗ r e c t C = new R e c t a n g l e ( 5 , 6 ) ; Classes
Pointers

c o u t << " r e c t A ␣ a r e a : ␣ " << r e c t A . a r e a ( ) << e n d l ; Arrays


Pointers to structures
c o u t << " r e c t B ␣ a r e a : ␣ " << r e c t B −>a r e a ( ) << e n d l ; Pointers to classes
c o u t << " r e c t C ␣ a r e a : ␣ " << r e c t C −>a r e a ( ) << e n d l ;
delete rectB ;
delete rectC ;
return 0;
}

1.45

You might also like