SlideShare a Scribd company logo
DATA STRUCTURES AND ALGORITHMS
LAB 2

Bianca Tesila
FILS, Feb 2014
OBJECTIVES
Transition from C to C++
 Struct vs. classes in C++
 Templates

WHY C++?





C + + allows the implementation of data
structures with generic data types via templates.

C follows the procedural programming paradigm
while C++ can follow both procedural paradigm
and OOP (Which are the OOP principles?)
TRANSITION FROM C TO C++








Any program written in C (“.c” extension) can be compiled by
a C++ compiler (“.cpp” extension); not vice versa
In C we don’t have classes!!!
The NAMESPACE feature in C++ is absent in case of C:
avoid name collisions (namespaces are similar to Java
packages- you can look for differences & similarities for the
next time)
Standard input & output functions differ in the two
languages: in C, we have scanf and printf; in C++ we have
cin>> and cout<<
TRANSITION FROM C TO C++
‼ Exercise:

Implement a function to sort an array of 5 elements of type
double. Use a swap mechanism, which has to be
implemented in another function.

Hint: Pay attention to passing by value/ passing by reference!
Do you remember which is the difference between them?
STRUCTURES VS CLASSES: STRUCTURES IN C++




We can define functions inside a structure; we can access the structure’s fields
by using this-><field_name>
C++ structures support inheritance
Everything inside a structure is public by default

Example:
typedef struct complex {
double re;
double im;
void complex_initialize(double param_re, double param_im) {
this->re = param_re;
this->im = param_im;
}
struct complex complex_conjugate() {
struct complex conjugate;
conjugate.complex_initialize(this->re, -(this->im));
return conjugate;
}
}complex;
STRUCTURES VS. CLASSES
‼ Exercise: Add to the complex structure new functions for the
addition, division and multiplication of complex numbers.
STRUCTURES VS. CLASSES: CLASSES


What is a class?



What is an object?



Replace the keyword struct with the keyword class in the last exercise



C++ structures behave like C++ classes, allowing functions, contructors,
destructors. The main diffrence between classes and C++ structures is
that everything inside a structure is public, by default, while everything
inside a class is private, by default
STRUCTURES VS. CLASSES: CLASSES
‼ Exercise: Make a Complex class using the previous
exercise.
Hint:
Don’t forget about the fact that everything inside a class is
private, by default.
Take into account the Encapsulation principle!
TEMPLATE CLASSES






Templates are a feature of the C++ programming language that
allow functions and classes to operate with generic types. This
allows a function or class to work on many different data types
without being rewritten for each one.
Templates are of great utility to programmers in C++, especially
when combined with multiple inheritance and operator
overloading.

Similar to Java Generics

template<typename T> class class_name { ... }
 A normal class definition will be prefixed by template<typename T>
 The type T can now be used as a valid type within the class
 we can have variables, function arguments and function return
values of type T


Everything happens at compile time, not runtime



The compiler analyzes how you use the class
TEMPLATE CLASSES: EXAMPLE
template<typename T>
class KeyStorage
{
public:
int key;
T member; //a generic member: we don't know its type when creating the
class
};
int main()

{
//Everything happens to compile time, not to run time
//The compiler analyses the way in which you use the class
KeyStorage<long> keyElement1;
KeyStorage<int> keyElement2;

return 0;
}

‼ Exercise: Make a constructor, a destructor, a getter and a setter for the
template class KeyStorage.
HOMEWORK






Finish all the lab exercises.
Implement a template class for storing the coordinates of a
point. Add corresponding methods for moving a point(along
Ox, along Oy, along both Ox and Oy). Using the
implemented class, develop an application for moving a
point inside a rectangle of dimensions 1 x N, where N is
given by the user. Start from the origin.

Implement a template class for bank accounts with
corresponding methods(deposit, withdrawal, balance
display, display owner etc). Develop an application that
uses this class.
INTERVIEW:
Does overloading exist in C++?
 Do abstract classes exist in C++?
 When is it better to use abstract classes and
when templates?
 Do static classes exist in C++?


More Related Content

What's hot (19)

PDF
Generic Programming
Muhammad Alhalaby
 
PDF
Applications of stack
eShikshak
 
PPT
Scala functions
Knoldus Inc.
 
DOC
5.program structure
Shankar Gangaju
 
PPTX
DATA STRUCTURE - STACK
Devyani Chaudhari
 
ODP
Functional Programming With Scala
Knoldus Inc.
 
PPT
Templates
Nilesh Dalvi
 
PDF
Lec 6 14_aug [compatibility mode]
Palak Sanghani
 
RTF
Imp_Points_Scala
Nagavarunkumar Kolla
 
PDF
Lec16-CS110 Computational Engineering
Sri Harsha Pamu
 
PDF
Generic programming and concepts that should be in C++
Anton Kolotaev
 
PPTX
Computer programming 2 chapter 1-lesson 2
MLG College of Learning, Inc
 
PPTX
C language presentation
bainspreet
 
PPTX
Storage classes in c language
tanmaymodi4
 
ODP
Talk on Standard Template Library
Anirudh Raja
 
DOC
1183 c-interview-questions-and-answers
Akash Gawali
 
DOCX
Comp 220 ilab 7 of 7
ashhadiqbal
 
PPTX
MCRL2
kashif kashif
 
DOCX
Best,worst,average case .17581556 045
university of Gujrat, pakistan
 
Generic Programming
Muhammad Alhalaby
 
Applications of stack
eShikshak
 
Scala functions
Knoldus Inc.
 
5.program structure
Shankar Gangaju
 
DATA STRUCTURE - STACK
Devyani Chaudhari
 
Functional Programming With Scala
Knoldus Inc.
 
Templates
Nilesh Dalvi
 
Lec 6 14_aug [compatibility mode]
Palak Sanghani
 
Imp_Points_Scala
Nagavarunkumar Kolla
 
Lec16-CS110 Computational Engineering
Sri Harsha Pamu
 
Generic programming and concepts that should be in C++
Anton Kolotaev
 
Computer programming 2 chapter 1-lesson 2
MLG College of Learning, Inc
 
C language presentation
bainspreet
 
Storage classes in c language
tanmaymodi4
 
Talk on Standard Template Library
Anirudh Raja
 
1183 c-interview-questions-and-answers
Akash Gawali
 
Comp 220 ilab 7 of 7
ashhadiqbal
 
Best,worst,average case .17581556 045
university of Gujrat, pakistan
 

Viewers also liked (12)

PDF
Difference between c# generics and c++ templates
Umar Ali
 
PPTX
Data structures and algorithms lab11
Bianca Teşilă
 
PPTX
Data structures and algorithms lab8
Bianca Teşilă
 
PPTX
Data structures and algorithms lab1
Bianca Teşilă
 
PPTX
Data Structures - Lecture 7 [Linked List]
Muhammad Hammad Waseem
 
PPTX
Introduction to computer architecture and organization
Muhammad Ishaq
 
PPTX
Data structures and algorithms lab5
Bianca Teşilă
 
PPTX
USMLE and Canadian Exams
Kuwait Medical Society, UK & Ireland
 
PPTX
Chapter 1 introduction to computers
haider ali
 
PPT
0. Course Introduction
Intro C# Book
 
PPT
Ppt 1
shanmugamsara
 
PDF
DSA-2012-Lect00
Haitham El-Ghareeb
 
Difference between c# generics and c++ templates
Umar Ali
 
Data structures and algorithms lab11
Bianca Teşilă
 
Data structures and algorithms lab8
Bianca Teşilă
 
Data structures and algorithms lab1
Bianca Teşilă
 
Data Structures - Lecture 7 [Linked List]
Muhammad Hammad Waseem
 
Introduction to computer architecture and organization
Muhammad Ishaq
 
Data structures and algorithms lab5
Bianca Teşilă
 
USMLE and Canadian Exams
Kuwait Medical Society, UK & Ireland
 
Chapter 1 introduction to computers
haider ali
 
0. Course Introduction
Intro C# Book
 
DSA-2012-Lect00
Haitham El-Ghareeb
 
Ad

Similar to Data structures and algorithms lab2 (20)

PPTX
C++ language
Hamza Asif
 
PPT
c++ ppt.ppt
FarazKhan89093
 
PPT
lecture02-cpp.ppt
MZGINBarwary
 
PPTX
Data Types & Input/Output Streams.pptx — Description
sachinkumar541404
 
PPT
2CPP15 - Templates
Michael Heron
 
PDF
c-for-c-programmers.pdf
Salar32
 
PPT
lecture02-cpp.ppt
DevliNeeraj
 
PPT
lecture02-cpp.ppt
ssuser0c24d5
 
PPT
lecture02-cpp.ppt
nilesh405711
 
PPT
lecture02-cpp.ppt
YashpalYadav46
 
PPT
C cpluplus 2
sanya6900
 
PPTX
C++ Basics
Himanshu Sharma
 
PPT
lecture5-cpp.pptintroduccionaC++basicoye
quetsqrj
 
PPT
Introduction to Inheritance in C plus plus
University of Sindh
 
PPT
C++ - A powerful and system level language
dhimananshu130803
 
PPT
Oops lecture 1
rehan16091997
 
PPTX
C++ overview
Prem Ranjan
 
PDF
Object Oriented Programming using C++ - Part 5
University College of Engineering Kakinada, JNTUK - Kakinada, India
 
PPT
UsingCPP_for_Artist.ppt
vinu28455
 
PPTX
lecture NOTES ON OOPS C++ ON CLASS AND OBJECTS
NagarathnaRajur2
 
C++ language
Hamza Asif
 
c++ ppt.ppt
FarazKhan89093
 
lecture02-cpp.ppt
MZGINBarwary
 
Data Types & Input/Output Streams.pptx — Description
sachinkumar541404
 
2CPP15 - Templates
Michael Heron
 
c-for-c-programmers.pdf
Salar32
 
lecture02-cpp.ppt
DevliNeeraj
 
lecture02-cpp.ppt
ssuser0c24d5
 
lecture02-cpp.ppt
nilesh405711
 
lecture02-cpp.ppt
YashpalYadav46
 
C cpluplus 2
sanya6900
 
C++ Basics
Himanshu Sharma
 
lecture5-cpp.pptintroduccionaC++basicoye
quetsqrj
 
Introduction to Inheritance in C plus plus
University of Sindh
 
C++ - A powerful and system level language
dhimananshu130803
 
Oops lecture 1
rehan16091997
 
C++ overview
Prem Ranjan
 
Object Oriented Programming using C++ - Part 5
University College of Engineering Kakinada, JNTUK - Kakinada, India
 
UsingCPP_for_Artist.ppt
vinu28455
 
lecture NOTES ON OOPS C++ ON CLASS AND OBJECTS
NagarathnaRajur2
 
Ad

More from Bianca Teşilă (6)

PDF
Akka Streams - An Adobe data-intensive story
Bianca Teşilă
 
PPTX
Data structures and algorithms lab10
Bianca Teşilă
 
PPTX
Data structures and algorithms lab9
Bianca Teşilă
 
PPTX
Data structures and algorithms lab7
Bianca Teşilă
 
PPTX
Data structures and algorithms lab6
Bianca Teşilă
 
PPTX
Data structures and algorithms lab4
Bianca Teşilă
 
Akka Streams - An Adobe data-intensive story
Bianca Teşilă
 
Data structures and algorithms lab10
Bianca Teşilă
 
Data structures and algorithms lab9
Bianca Teşilă
 
Data structures and algorithms lab7
Bianca Teşilă
 
Data structures and algorithms lab6
Bianca Teşilă
 
Data structures and algorithms lab4
Bianca Teşilă
 

Recently uploaded (20)

PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PPTX
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PPTX
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
community health nursing question paper 2.pdf
Prince kumar
 
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 

Data structures and algorithms lab2

  • 1. DATA STRUCTURES AND ALGORITHMS LAB 2 Bianca Tesila FILS, Feb 2014
  • 2. OBJECTIVES Transition from C to C++  Struct vs. classes in C++  Templates 
  • 3. WHY C++?   C + + allows the implementation of data structures with generic data types via templates. C follows the procedural programming paradigm while C++ can follow both procedural paradigm and OOP (Which are the OOP principles?)
  • 4. TRANSITION FROM C TO C++     Any program written in C (“.c” extension) can be compiled by a C++ compiler (“.cpp” extension); not vice versa In C we don’t have classes!!! The NAMESPACE feature in C++ is absent in case of C: avoid name collisions (namespaces are similar to Java packages- you can look for differences & similarities for the next time) Standard input & output functions differ in the two languages: in C, we have scanf and printf; in C++ we have cin>> and cout<<
  • 5. TRANSITION FROM C TO C++ ‼ Exercise: Implement a function to sort an array of 5 elements of type double. Use a swap mechanism, which has to be implemented in another function. Hint: Pay attention to passing by value/ passing by reference! Do you remember which is the difference between them?
  • 6. STRUCTURES VS CLASSES: STRUCTURES IN C++    We can define functions inside a structure; we can access the structure’s fields by using this-><field_name> C++ structures support inheritance Everything inside a structure is public by default Example: typedef struct complex { double re; double im; void complex_initialize(double param_re, double param_im) { this->re = param_re; this->im = param_im; } struct complex complex_conjugate() { struct complex conjugate; conjugate.complex_initialize(this->re, -(this->im)); return conjugate; } }complex;
  • 7. STRUCTURES VS. CLASSES ‼ Exercise: Add to the complex structure new functions for the addition, division and multiplication of complex numbers.
  • 8. STRUCTURES VS. CLASSES: CLASSES  What is a class?  What is an object?  Replace the keyword struct with the keyword class in the last exercise  C++ structures behave like C++ classes, allowing functions, contructors, destructors. The main diffrence between classes and C++ structures is that everything inside a structure is public, by default, while everything inside a class is private, by default
  • 9. STRUCTURES VS. CLASSES: CLASSES ‼ Exercise: Make a Complex class using the previous exercise. Hint: Don’t forget about the fact that everything inside a class is private, by default. Take into account the Encapsulation principle!
  • 10. TEMPLATE CLASSES    Templates are a feature of the C++ programming language that allow functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one. Templates are of great utility to programmers in C++, especially when combined with multiple inheritance and operator overloading. Similar to Java Generics template<typename T> class class_name { ... }  A normal class definition will be prefixed by template<typename T>  The type T can now be used as a valid type within the class  we can have variables, function arguments and function return values of type T  Everything happens at compile time, not runtime  The compiler analyzes how you use the class
  • 11. TEMPLATE CLASSES: EXAMPLE template<typename T> class KeyStorage { public: int key; T member; //a generic member: we don't know its type when creating the class }; int main() { //Everything happens to compile time, not to run time //The compiler analyses the way in which you use the class KeyStorage<long> keyElement1; KeyStorage<int> keyElement2; return 0; } ‼ Exercise: Make a constructor, a destructor, a getter and a setter for the template class KeyStorage.
  • 12. HOMEWORK    Finish all the lab exercises. Implement a template class for storing the coordinates of a point. Add corresponding methods for moving a point(along Ox, along Oy, along both Ox and Oy). Using the implemented class, develop an application for moving a point inside a rectangle of dimensions 1 x N, where N is given by the user. Start from the origin. Implement a template class for bank accounts with corresponding methods(deposit, withdrawal, balance display, display owner etc). Develop an application that uses this class.
  • 13. INTERVIEW: Does overloading exist in C++?  Do abstract classes exist in C++?  When is it better to use abstract classes and when templates?  Do static classes exist in C++? 