SlideShare a Scribd company logo
1 of 12
 User-Defined Types
 Structures
 Classes
 Enumerations
 Fundamental Types
 short, int, long
 char
 bool
 float, double
 User-defined types
 Structure
 Class
 Enumerator
1. struct Vector
2. {
3. int sz; // Number of elements
4. double* elem; // Pointer to elements
5. };
1. void f(Vector v, Vector& rv, Vector* pv)
2. {
3. int i1 = v.sz; // Access through name
4. int i2 = rv.sz; // Access through reference
5. int i4 = pv->sz; // Access through pointer
6. }
1. void vector_init(Vector& v, int s)
2. {
3. v.elem = new double[s];
4. v.sz = s;
5. }
1. // Read s integers from cin and return their
2. // sum; s is assumed to be positive.
3. double read_and_sum(int s)
4. {
5. Vector v;
6. vector_init(v, s); // Allocate s elements for v
7. for (int i=0; i!=s; ++i)
8. cin >> v.elem[i];
9.
10. double sum = 0;
11. for (int i=0; i!=s; ++i)
12. sum += v.elem[i];// Take the sum of the elements
13.
14. return sum;
15. }
1. class Vector
2. {
3. public:
4. Vector(int s) : elem(new double[s]), sz(s) {}
5. double& operator[](int i) { return elem[i]; }
6. int size() { return sz; }
7.
8. private:
9. double* elem; // Pointer to the elements
10. int sz; // The number of elements
11. };
1. class Vector
2. {
3. public:
4. Vector(int s) : elem(new double[s]), sz(s) {}
5. double& operator[](int i) { return elem[i]; }
6. int size() { return sz; }
7.
8. private:
9. double* elem; // Pointer to the elements
10. int sz; // The number of elements
11. };
1. enum Color { red, blue, green };
2. enum Traffic_light { green, yellow, red };
3.
4. Color col = Color::red;
5. Traffic_light light = Traffic_light::red;
1. // Prefix increment: ++
2. Traffic_light& operator++(Traffic_light& t)
3. {
4. switch (t)
5. {
6. case Traffic_light::green: return t=Traffic_light::yellow;
7. case Traffic_light::yellow: return t=Traffic_light::red;
8. case Traffic_light::red: return t=Traffic_light::green;
9. }
10. }
11.
12. Traffic_light next = ++light; // next becomes green
5. struct, class, enum

More Related Content

What's hot (19)

POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
 
Session 4
Session 4Session 4
Session 4
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
pointers
pointerspointers
pointers
 
Ds06 linked list- insert a node at beginning
Ds06   linked list- insert a node at beginningDs06   linked list- insert a node at beginning
Ds06 linked list- insert a node at beginning
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 
Array & Exception Handling in C# (CSharp)
Array & Exception Handling in C# (CSharp)Array & Exception Handling in C# (CSharp)
Array & Exception Handling in C# (CSharp)
 
Unit v
Unit vUnit v
Unit v
 
Function pointer
Function pointerFunction pointer
Function pointer
 
Unit iii
Unit iiiUnit iii
Unit iii
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Computer Science:Pointers in C
Computer Science:Pointers in CComputer Science:Pointers in C
Computer Science:Pointers in C
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
 
Chp3(pointers ref)
Chp3(pointers ref)Chp3(pointers ref)
Chp3(pointers ref)
 
C intro
C introC intro
C intro
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++
 

Similar to 5. struct, class, enum

Chap 2 Arrays and Structures.ppt
Chap 2  Arrays and Structures.pptChap 2  Arrays and Structures.ppt
Chap 2 Arrays and Structures.pptshashankbhadouria4
 
Chap 2 Arrays and Structures.pptx
Chap 2  Arrays and Structures.pptxChap 2  Arrays and Structures.pptx
Chap 2 Arrays and Structures.pptxshashankbhadouria4
 
F# Eye For The C# Guy - Seattle 2013
F# Eye For The C# Guy - Seattle 2013F# Eye For The C# Guy - Seattle 2013
F# Eye For The C# Guy - Seattle 2013Phillip Trelford
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Abu Saleh
 
MinOfFourNumbers.javaimport java.util.Scanner;public class MinOf.pdf
MinOfFourNumbers.javaimport java.util.Scanner;public class MinOf.pdfMinOfFourNumbers.javaimport java.util.Scanner;public class MinOf.pdf
MinOfFourNumbers.javaimport java.util.Scanner;public class MinOf.pdfaptex1
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxRashidFaridChishti
 
VIT351 Software Development VI Unit3
VIT351 Software Development VI Unit3VIT351 Software Development VI Unit3
VIT351 Software Development VI Unit3YOGESH SINGH
 
This class maintains a list of 4 integers. This list .docx
 This class maintains a list of 4 integers.   This list .docx This class maintains a list of 4 integers.   This list .docx
This class maintains a list of 4 integers. This list .docxKomlin1
 
Engineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptxEngineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptxhappycocoman
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10Vince Vo
 
C++ Object Oriented Programming Lecture Slides for Students
C++ Object Oriented Programming Lecture Slides for StudentsC++ Object Oriented Programming Lecture Slides for Students
C++ Object Oriented Programming Lecture Slides for StudentsMuhammadAli224595
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfsudhakargeruganti
 
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdfLab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdfQalandarBux2
 
Use C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdfUse C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdfshalins6
 
write recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdfwrite recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdfarpitcomputronics
 

Similar to 5. struct, class, enum (20)

Chap 2 Arrays and Structures.ppt
Chap 2  Arrays and Structures.pptChap 2  Arrays and Structures.ppt
Chap 2 Arrays and Structures.ppt
 
Chap 2 Arrays and Structures.pptx
Chap 2  Arrays and Structures.pptxChap 2  Arrays and Structures.pptx
Chap 2 Arrays and Structures.pptx
 
F# Eye For The C# Guy - Seattle 2013
F# Eye For The C# Guy - Seattle 2013F# Eye For The C# Guy - Seattle 2013
F# Eye For The C# Guy - Seattle 2013
 
C1320prespost
C1320prespostC1320prespost
C1320prespost
 
C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 
MinOfFourNumbers.javaimport java.util.Scanner;public class MinOf.pdf
MinOfFourNumbers.javaimport java.util.Scanner;public class MinOf.pdfMinOfFourNumbers.javaimport java.util.Scanner;public class MinOf.pdf
MinOfFourNumbers.javaimport java.util.Scanner;public class MinOf.pdf
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
 
VIT351 Software Development VI Unit3
VIT351 Software Development VI Unit3VIT351 Software Development VI Unit3
VIT351 Software Development VI Unit3
 
8. abstract types
8. abstract types8. abstract types
8. abstract types
 
This class maintains a list of 4 integers. This list .docx
 This class maintains a list of 4 integers.   This list .docx This class maintains a list of 4 integers.   This list .docx
This class maintains a list of 4 integers. This list .docx
 
Engineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptxEngineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptx
 
Pointers and arrays
Pointers and arraysPointers and arrays
Pointers and arrays
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
 
C++ Object Oriented Programming Lecture Slides for Students
C++ Object Oriented Programming Lecture Slides for StudentsC++ Object Oriented Programming Lecture Slides for Students
C++ Object Oriented Programming Lecture Slides for Students
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
 
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdfLab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
 
Use C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdfUse C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdf
 
write recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdfwrite recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdf
 

More from Vahid Heidari

17. thread and deadlock
17. thread and deadlock17. thread and deadlock
17. thread and deadlockVahid Heidari
 
15. map, unordered map, algorithms
15. map, unordered map, algorithms15. map, unordered map, algorithms
15. map, unordered map, algorithmsVahid Heidari
 
14. containers, vector, list
14. containers, vector, list14. containers, vector, list
14. containers, vector, listVahid Heidari
 
13. string, io streams
13. string, io streams13. string, io streams
13. string, io streamsVahid Heidari
 
12. standard library introduction
12. standard library introduction12. standard library introduction
12. standard library introductionVahid Heidari
 
9. class hierarchies
9. class hierarchies9. class hierarchies
9. class hierarchiesVahid Heidari
 
7. abstraction mechanisms, containers
7. abstraction mechanisms, containers7. abstraction mechanisms, containers
7. abstraction mechanisms, containersVahid Heidari
 
6. separation, namespace, error
6. separation, namespace, error6. separation, namespace, error
6. separation, namespace, errorVahid Heidari
 
2. types, vars, arith, consts
2. types, vars, arith, consts2. types, vars, arith, consts
2. types, vars, arith, constsVahid Heidari
 
1. preface, hello world
1. preface, hello world1. preface, hello world
1. preface, hello worldVahid Heidari
 

More from Vahid Heidari (16)

18. utilities
18. utilities18. utilities
18. utilities
 
17. thread and deadlock
17. thread and deadlock17. thread and deadlock
17. thread and deadlock
 
16. smart pointers
16. smart pointers16. smart pointers
16. smart pointers
 
15. map, unordered map, algorithms
15. map, unordered map, algorithms15. map, unordered map, algorithms
15. map, unordered map, algorithms
 
14. containers, vector, list
14. containers, vector, list14. containers, vector, list
14. containers, vector, list
 
13. string, io streams
13. string, io streams13. string, io streams
13. string, io streams
 
12. standard library introduction
12. standard library introduction12. standard library introduction
12. standard library introduction
 
11. template
11. template11. template
11. template
 
10. copy and move
10. copy and move10. copy and move
10. copy and move
 
9. class hierarchies
9. class hierarchies9. class hierarchies
9. class hierarchies
 
7. abstraction mechanisms, containers
7. abstraction mechanisms, containers7. abstraction mechanisms, containers
7. abstraction mechanisms, containers
 
6. separation, namespace, error
6. separation, namespace, error6. separation, namespace, error
6. separation, namespace, error
 
4. pointers, arrays
4. pointers, arrays4. pointers, arrays
4. pointers, arrays
 
3. tests, loops
3. tests, loops3. tests, loops
3. tests, loops
 
2. types, vars, arith, consts
2. types, vars, arith, consts2. types, vars, arith, consts
2. types, vars, arith, consts
 
1. preface, hello world
1. preface, hello world1. preface, hello world
1. preface, hello world
 

Recently uploaded

Software Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements EngineeringSoftware Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements EngineeringPrakhyath Rai
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Henry Schreiner
 
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Andreas Granig
 
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024MulesoftMunichMeetup
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024SimonedeGijt
 
Encryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key ConceptsEncryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key Conceptsthomashtkim
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConNatan Silnitsky
 
architecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdfarchitecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdfWSO2
 
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale IbridaUNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale IbridaNeo4j
 
From Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIFrom Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIInflectra
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Maxim Salnikov
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypseTomasz Kowalczewski
 
Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMarkus Moeller
 
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfThe Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfkalichargn70th171
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Clinic
 
Rapidoform for Modern Form Building and Insights
Rapidoform for Modern Form Building and InsightsRapidoform for Modern Form Building and Insights
Rapidoform for Modern Form Building and Insightsrapidoform
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...naitiksharma1124
 

Recently uploaded (20)

Software Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements EngineeringSoftware Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements Engineering
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024
 
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
 
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
 
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
 
Encryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key ConceptsEncryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key Concepts
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeCon
 
architecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdfarchitecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdf
 
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale IbridaUNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
 
From Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIFrom Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST API
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?
 
Abortion Pill Prices Mthatha (@](+27832195400*)[ 🏥 Women's Abortion Clinic In...
Abortion Pill Prices Mthatha (@](+27832195400*)[ 🏥 Women's Abortion Clinic In...Abortion Pill Prices Mthatha (@](+27832195400*)[ 🏥 Women's Abortion Clinic In...
Abortion Pill Prices Mthatha (@](+27832195400*)[ 🏥 Women's Abortion Clinic In...
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
 
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
 
Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdf
 
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfThe Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
 
Rapidoform for Modern Form Building and Insights
Rapidoform for Modern Form Building and InsightsRapidoform for Modern Form Building and Insights
Rapidoform for Modern Form Building and Insights
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
 

5. struct, class, enum

  • 1.
  • 2.  User-Defined Types  Structures  Classes  Enumerations
  • 3.  Fundamental Types  short, int, long  char  bool  float, double  User-defined types  Structure  Class  Enumerator
  • 4. 1. struct Vector 2. { 3. int sz; // Number of elements 4. double* elem; // Pointer to elements 5. };
  • 5. 1. void f(Vector v, Vector& rv, Vector* pv) 2. { 3. int i1 = v.sz; // Access through name 4. int i2 = rv.sz; // Access through reference 5. int i4 = pv->sz; // Access through pointer 6. }
  • 6. 1. void vector_init(Vector& v, int s) 2. { 3. v.elem = new double[s]; 4. v.sz = s; 5. }
  • 7. 1. // Read s integers from cin and return their 2. // sum; s is assumed to be positive. 3. double read_and_sum(int s) 4. { 5. Vector v; 6. vector_init(v, s); // Allocate s elements for v 7. for (int i=0; i!=s; ++i) 8. cin >> v.elem[i]; 9. 10. double sum = 0; 11. for (int i=0; i!=s; ++i) 12. sum += v.elem[i];// Take the sum of the elements 13. 14. return sum; 15. }
  • 8. 1. class Vector 2. { 3. public: 4. Vector(int s) : elem(new double[s]), sz(s) {} 5. double& operator[](int i) { return elem[i]; } 6. int size() { return sz; } 7. 8. private: 9. double* elem; // Pointer to the elements 10. int sz; // The number of elements 11. };
  • 9. 1. class Vector 2. { 3. public: 4. Vector(int s) : elem(new double[s]), sz(s) {} 5. double& operator[](int i) { return elem[i]; } 6. int size() { return sz; } 7. 8. private: 9. double* elem; // Pointer to the elements 10. int sz; // The number of elements 11. };
  • 10. 1. enum Color { red, blue, green }; 2. enum Traffic_light { green, yellow, red }; 3. 4. Color col = Color::red; 5. Traffic_light light = Traffic_light::red;
  • 11. 1. // Prefix increment: ++ 2. Traffic_light& operator++(Traffic_light& t) 3. { 4. switch (t) 5. { 6. case Traffic_light::green: return t=Traffic_light::yellow; 7. case Traffic_light::yellow: return t=Traffic_light::red; 8. case Traffic_light::red: return t=Traffic_light::green; 9. } 10. } 11. 12. Traffic_light next = ++light; // next becomes green

Editor's Notes

  1. در این ویدیو در مورد type هایی که کاربر یا برنامه نویس میتونه تعریف کنه صحبت می کنیم. برنامه نویس 3 جور type میتونه تعریف کنه. Structure class و enumeration
  2. در ویدیو های قبلی در مورد fundamental typeها صحبت کردیم و اونا رو معرفی کردیم ولی C++ اجازه میده که علاوه بر fundamental typeها یکسری typeهای دیگه هم تعریف کنیم که جزء انواع تعریف شده ی پایه ای نباشند مثلا structure و کلاس و enumerationها
  3. اولین قدم برای این که یک type جدید ایجاد کنیم اینه که عناصری که نیاز داریم رو در کنار هم جمع کنیم و به نوعی سازماندهی کنیم به اصطلاع یک data structure بسازیم. به این data structure در C++ به اختصار میگیم struct . یک Declaration از struct رو با هم ببینیم. کلمه ی struct مشخص می کنه که می خوایم یک type جدید بسازیم و جلوی struct اسم اون type جدید رو می نویسیم. با یک آکولاد باز و بسته محدوده ی struct رو تعریف می کنیم و داخل اون typeهایی که نیاز داریم رو declare میکنیم. در انتهای struct هم حتما باید یک سمی کلون بذاریم.این type جدید اسمش vector هست و داخل اون یک sz به معنی size هست و یک pointer به نام elem از نوع double وجود داره. Vector یا بردار مثل آرایه هست که در شکل مبینید. Sz تعداد عناصر رو نشون میده و elem هم به ابتدای آرایه اشاره میکنه.
  4. برای دسترسی داشتن به اعضای struct باید از اپراتور . استفاده کنیم یعنی جلوی اسم متغییر از نوع struct یک . بذاریم و اسم عنصری که می خوایم رو جلوش بنویسیم. در این مثال میبینید یک تابع به نام f داریم که خروجی اون از نوع void هست یعنی هیچی برنمی گردونه و 3 تا ورودی میگره. ورودی اول یک Vectorهست. ورودی دوم یک reference به یک Vector هست و ورودی آخر یک اشاره گر به یک Vector. در بدنه تابع 3 تا متغییر از نوع int تعریف کردیم و هر کدوم رو مقدار دهیم کردیم. همون طور که میبینید برای دسترسی داشتن به اعضای struct در مورد خود struct و reference به یک struct مشابه هم عمل می کنیم چون reference مثل خود اون متغییر هست. ولی برای pointer چون باید به صورت غیر مستقیم دسترسی پیدا کنیم از اپراتور -> استفاده می کنیم. اوپراتور فلش یه حسی از اینکه pv متغییر نیست و داره به یه جایی اشاره میکنه رو ایجاد میکنه.
  5. در این مثال یک تابع داریم به نام vector_init که خروجی اون از نوع void هست یعنی هیچ خروجی نداره و 2 تا ورودی داره. اولی یک reference به یک Vector هست و دومی یک عدد صحیح هست. در بدنه تابع خط 3 یک اپراتور داریم به نام New. اپراتور new کارش اینه که هر چی جلوش بود رو در حافظه میسازه و یک اشاره گر به آدرس اون برمی گردونه. یعنی یک آرایه از نوع double می سازه که اندازه اون برابر s هست که از ورودی دریافت کردیم. فرض کنید توی s عدد 5 باشه. پس یک آرایه 5 تایی از double میسازه و آدرس شروع اون رو بر میگردونه که ما اون رو در elem ذخیره میکنیم. در خط بعد هم s رو توی sz یا size میریزیم یعنی تعداد عناصر آرایه که با new ساختیم رو در sz ذخیره می کنیم پس این تابع یک reference از یک vector به همراه یک size میگیره و یک vector برای ما میسازه. الان یک vector داریم که آدرس شروع اولین عنصر اون در elem ذخیره شده و تعداد عناصر اون در sz دخیره شده.
  6. یک مثال دیگه از struct با هم می بینیم در این مثال می خوایم از تابعی که در اسلاید قبل دیدیم استفاده کنیم. در این تابع یک vector رو declare کردیم به نام v و بعد اونو به تابع vector_init دادیم تا برای ما اونو بسازه. از اونجایی که اولین ورودی تابع vector_init یک reference به یک vector بود میتونیم خود v رو به عنوان ورودی بدیم به vector_init و هر کاری که تابع روی ورودی انجام میده مستقیما روی v هم انجام میشه. در حلقه for از ورودی به تعداد عناصر آرایه با استفاده از cin یک عدد اعشاری رو از ورودی خوندیم و ریختیم توی elem و به ترتیب اونو پر کردیم. در حلقه دوم هم تمام مقادیری که از ورودی گرفتیم با هم جمع کردیم و حاصل جمع همه ی عناصر vector رو به عنوان خروجی برگردوندیم.
  7. تا اینجا ما با استفاده از struct تونستیم یک type جدید بسازیم و عناصر مورد نیاز برای نگه داری یک سری اطلاعات رو در اون ذخیره کنیم. و برای اینکه روی اون یک سری عملیات انجام بدیم چند تا تابع نوشتیم. حالا همه ی اون کار ها رو با استفاده از کلاس انجام میدیم. کلاس به ما امکان میده که کنترول بیشتری روی اعضای اون داشته باشیم و یکسری از کارهایی فقط مخصوص کلاسه رو اعضای کلاس انجام بدیم در حالی که از دید هر کسی که بیرون کلاسه مخفی نگه داشته میشن. هر کلاس میتونه از یک مجموعه از اعضا تشکیل شده. این اعضا می تونه شامل memberها و functionها باشند. به functionهای کلاس method میگیم. با استفاده از کلمه public و private سطح دسترسی به اون memberها رو مشخص می کنیم. اعضای public کلاس از بیرون کلاس برای همه قابل دسترسی هستند و اعضای private فقط داخل کلاس قابل دسترسی هستند و کسی غیر از کلاس نمی تونه اونا رو دستکاری کنه.
  8. یک تابع در کلاس داریم به نام constructor که از اون برای مقدار اولیه دادن به اعضای کلاس استفاده میشه. کاری که constructor میکنه مثل تابع vector_init هست که با هم بررسی کردیم. تابع constructor باید همنام با اسم کلاس باشه و نوع خروجی هم برای اون مشخص نمی کنیم حتی void هم نباید بنویسیم. یک اوپراتور برای کلاس تعریف کردیم که با استفاده از اون میتونیم مثل آرایه به عناصر داخل کلاس دسترسی داشته باشیم. اسم این اپراتور subscript هست و با یک عدد به نام i به عنصر iام elem دسترسی پیدا می کنیم. شما می تونید برای یک کلاس تمام اپراتوری ها مثل جمع و تفریق و غیره رو با کلمه ی operator و بعد نوشتن اون اپراتور تعریف کنید. با استفاده از متد size هم می تونیم تعداد عناصر موجود در vector رو ببینیم.
  9. یکی دیگه از typeهایی که برنامه نویس میتونه ایجاد کنه enum یا متغییر های شمارشیه. با کمک enum یکسری مقادیر به هم مرتبط رو دسته بندی می کنیم و برای اون ها اسم میگذاریم. مثلا می تونیم برای رنگها یک enum به نام color ایجاد کنیم و برای هر رنگ اسم بذاریم. یا برای چراغ راهنمایی اسم بذاریم و یک enum به نام traffic light ایجاد کنیم و برای هر رنگ چراغ اسم بذاریم. برای استفاده از enum هم به این صورت باید عمل کنیم. برای مقدار دهی به یک enum باید از :: بعد از اسم enum استفاده کنیم و بعد :: مقداری که میخوایم رو مینویسیم.
  10. یک مثال از عملیاتهایی که می شه روی enum انجام داد. مثلا می تونیم یک اپراتور برای ++ کردن بنویسیم. با استفاده از switch case که قبلا یاد گرقتیم ورودی رو با مقادیر enum چک می کنیم و رنگ چراغ بعدی رو بر میگردونیم. نحوه ی استفاده از اپراتور رو در خط 12 می بینید.
  11. در پایان یک جمع بندی بکنیم. در این ویدیو در مورد typeهایی که برنامه نویس میتونه تعریف کنه و با استفاده از اونا عملیاتهای جدیدی رو تعریف کنه رو بررسی کردیم با struct و کلاس آشنا شدیم و گفتیم که در کلاس میشه با استفاده از private و public کردن اعضای کلاس نحوه ی دسترسی داشتن با اعضا رو کنترول کرد. و همینطور enum رو معرفی کردیم و کاربرد اون رو با هم دیدیم. امید وارم مفید بوده باشه با تشکر