SlideShare a Scribd company logo
1 of 14
For any help regarding CPP Homework Help
Visit :- https://www.cpphomeworkhelp.com/ ,
Email :- info@cpphomeworkhelp.com or
Call us at :- +1 678 648 4277
CPP Homework Help
1 Additional Material
1.1 Templates and Header Files
The compiler does not compile a templated function until it encounters a use of it –
until that function is used with a particular type parameter, at which point it is
compiled for that type parameter. Because of this, if you define a templated class in
a header file and implement its (templated) functions in a .cpp file, the code in the
.cpp file will never get compiled unless you use the templated functions within that
.cpp file. To solve this problem, templated classes and functions are generally just
implemented in the header file, with no .cpp file.
1.2 Templates and friend Functions
There are some syntax oddities when using friend functions with templated
classes. In order to declare the function as a friend of the class, you need a
function prototype (or the full function definition) to appear before the class
definition. This is a problem, because you’ll often need to have the class already
defined in order for the function prototype to make sense. To get around this issue,
when writing friend functions with templated classes, you should include
declarations in the following order:
Problem
CPP Homework Help
1. A templated forward declaration of the class. (A forward declaration is a statement
like class SomeClass;, with no class body, that just alerts the compiler that the class
definition is coming.)
2. A templated function prototype for the friend function (or the entire function definition).
3. The full templated class definition, including the friend statement. When you write the
name of the function in the friend statement, you need to include an extra <> after it
to indicate that it is a templated function (e.g., friend MyClass operator+<>(const
MyClass &c1, const MyClass &c2;).
4. The operator function definition, if you didn’t include it above.
2 Multi-Type min
2.1 Using templates, implement a min function which returns the minimum of two
elements of any comparable type (i.e., it takes two arguments of some type T, and
works as long as values of type T can be compared with the < operator).
(To test this function, you may need to omit your usual using namespace std;
line, since there is already an std::min function.)
CPP Homework Help
2.2
Implement the min functionality from part 1 using only preprocessor macros. (Hint: You
will probably need the ternary operator – the ?: syntax.)
3 Casting
Assume you implemented Problem 5 from Problem Set 3 correctly. This would mean you
would have a working Polygon class, and inheriting from that a Triangle class and a
Rectangle class. Now imagine you have a pointer declared as Rectangle *rect; that
has been properly initialized.
3.1
Write a line of code showing how you would cast rect to a Triangle * without
checking for type correctness (i.e., without checking whether it actually points to a
Triangle). Do not use C-style casts.
3.2
Now write a line of code that does the same thing, but checks for type correctness
and throws an exception or returns a null pointer if rect does not actually point to a
Triangle.
CPP Homework Help
4 Templated Stack
A stack data structure stores a set of items, and allows accessing them via the
following operations:
• Push – add a new item to the stack
• Pop – remove the most recently added item that is still in the stack (i.e. that has not
yet been popped)
Top – Retrieve
For more explanation, see http://en.wikipedia.org/wiki/Stack (data structure).
4.1
Using templates, implement a Stack class that can be used to store items of any type.
You do not need to implement any constructors or destructors; the default constructor
should be sufficient, and you will not need to use new. Your class should support the
following 3 public functions (assuming T is the parameterized type which the Stack is
storing):
CPP Homework Help
• bool Stack::empty() – returns whether the stack is empty
• void Stack::push(const T &item) – adds item to the stack
• T &Stack::top() – returns a reference to the most-recently-added item
• void Stack::pop() – removes the most-recently-added item from the stack
Use an STL vector, deque, or list to implement your Stack. You may not use the
STL stack class. Make the member functions const where appropriate, and add
const/nonconst versions of each function for which it is appropriate. In the case
where the Stack is empty, pop should do nothing, and top should behave exactly
as normal (this will of course cause an error).
When working with templated classes, you cannot separate the function
implementations into a separate .cpp file; put all your code in the class definition.
(Hint: You can represent pushing by adding to the end of your vector, and popping
by removing the last element of the vector. This ensures that the popped item is
always the most recently inserted one that has not yet been popped.)
4.2 friend Functions and Operator Overloading (Optional)
CPP Homework Help
Make a friend function that implements a + operator for Stacks. The behavior of the +
operator should be such that when you write a + b, you get a new stack containing a’s
items followed by b’s items (assuming a and b are both Stacks), in their original order.
Thus, in the following example, the contents of c would be the same as if 1, 2, 3, and 4
had been pushed onto it in that order. (c.top() would therefore return the value 4.)
Put your code for this section in the same file as for the previous one.
5 Graph Representation (Optional)
A graph is a mathematical data structure consisting of nodes and edges connecting
them. To help you visualize it, you can think of a graph as a map of “cities” (nodes) and
“roads” (edges) connecting them. In an directed graph, the direction of the edge matters
– that is, an edge from A to B is not also an edge from B to A. You can read more at
Wikipedia: http://en.wikipedia.org/wiki/Graph (mathematics).
CPP Homework Help
One way to represent a graph is by assigning each node a unique ID number.
Then, for each node ID n, you can store a list of node ID’s to which n has an outgoing
edge. This list is called an adjacency list.
Write a Graph class that uses STL containers (vectors, maps, etc.) to represent a
directed graph. Each node should be represented by a unique integer (an int). Provide
the following member functions:
• Graph::Graph(const vector &starts, const vector &ends) Constructs a Graph with
the given set of edges, where starts and ends represent the ordered list of edges’
start and endpoints. For instance, consider the following graph:
The vectors used to initialize a Graph object representing this graph would be:
start: 1 1 1 5 5 4
end: 2 3 4 4 2 2
• int Graph::numOutgoing(const int nodeID) const Returns the number of outgoing
edges from nodeID – that is, edges with nodeID as the start point
• const vector &Graph::adjacent(const int nodeID) const Returns a reference to the
list of nodes to which nodeID has outgoing edges
CPP Homework Help
(Hint: Use the following data type to associate adjacency lists with node ID’s: map >. This
will also allow for easy lookup of the adjacency list for a given node ID. Note that the []
operator for maps inserts the item if it isn’t already there.) The constructor should throw an
invalid argument exception of the two vectors are not the same length. (This standard
exception class is defined in header file stdexcept.) The other member functions should do
likewise if the nodeID provided does not actually exist in the graph.
CPP Homework Help
Solutions
2 Multi-Type min
2.1
2.2
3 Casting
3.1
CPP Homework Help
3.2
4 Templated Stack
4.1
CPP Homework Help
5 Graph Representation
CPP Homework Help
CPP Homework Help
CPP Homework Help

More Related Content

Similar to CPP homework help

CSC8503 Principles of Programming Languages Semester 1, 2015.docx
CSC8503 Principles of Programming Languages Semester 1, 2015.docxCSC8503 Principles of Programming Languages Semester 1, 2015.docx
CSC8503 Principles of Programming Languages Semester 1, 2015.docxfaithxdunce63732
 
Educational Objectives After successfully completing this assignmen.pdf
Educational Objectives After successfully completing this assignmen.pdfEducational Objectives After successfully completing this assignmen.pdf
Educational Objectives After successfully completing this assignmen.pdfrajeshjangid1865
 
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docxCS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docxfaithxdunce63732
 
Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06Barry DeCicco
 
Chapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer ScienceChapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer ScienceKrithikaTM
 
Lecture 01 variables scripts and operations
Lecture 01   variables scripts and operationsLecture 01   variables scripts and operations
Lecture 01 variables scripts and operationsSmee Kaem Chann
 
Intro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & ArraysIntro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & ArraysBlue Elephant Consulting
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1stConnex
 
Gráficas en python
Gráficas en python Gráficas en python
Gráficas en python Jhon Valle
 
Lecture 19 matlab_script&function_files06
Lecture 19 matlab_script&function_files06Lecture 19 matlab_script&function_files06
Lecture 19 matlab_script&function_files06Aman kazmi
 
Lab 13 Practicing STLVector Container1a. Write a template func.pdf
Lab 13 Practicing STLVector Container1a. Write a template func.pdfLab 13 Practicing STLVector Container1a. Write a template func.pdf
Lab 13 Practicing STLVector Container1a. Write a template func.pdfinfo673628
 
Acm aleppo cpc training ninth session
Acm aleppo cpc training ninth sessionAcm aleppo cpc training ninth session
Acm aleppo cpc training ninth sessionAhmad Bashar Eter
 
Presentation 5th
Presentation 5thPresentation 5th
Presentation 5thConnex
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxAbhishek Tirkey
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxGauravPandey43518
 

Similar to CPP homework help (20)

CSC8503 Principles of Programming Languages Semester 1, 2015.docx
CSC8503 Principles of Programming Languages Semester 1, 2015.docxCSC8503 Principles of Programming Languages Semester 1, 2015.docx
CSC8503 Principles of Programming Languages Semester 1, 2015.docx
 
Educational Objectives After successfully completing this assignmen.pdf
Educational Objectives After successfully completing this assignmen.pdfEducational Objectives After successfully completing this assignmen.pdf
Educational Objectives After successfully completing this assignmen.pdf
 
MatlabIntro (1).ppt
MatlabIntro (1).pptMatlabIntro (1).ppt
MatlabIntro (1).ppt
 
Python - Lecture 12
Python - Lecture 12Python - Lecture 12
Python - Lecture 12
 
Matlab Manual
Matlab ManualMatlab Manual
Matlab Manual
 
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docxCS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
 
Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06
 
Lab Manual-OOP.pdf
Lab Manual-OOP.pdfLab Manual-OOP.pdf
Lab Manual-OOP.pdf
 
Chapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer ScienceChapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer Science
 
Lecture 01 variables scripts and operations
Lecture 01   variables scripts and operationsLecture 01   variables scripts and operations
Lecture 01 variables scripts and operations
 
Intro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & ArraysIntro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & Arrays
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
Gráficas en python
Gráficas en python Gráficas en python
Gráficas en python
 
Lecture 19 matlab_script&function_files06
Lecture 19 matlab_script&function_files06Lecture 19 matlab_script&function_files06
Lecture 19 matlab_script&function_files06
 
Lab 13 Practicing STLVector Container1a. Write a template func.pdf
Lab 13 Practicing STLVector Container1a. Write a template func.pdfLab 13 Practicing STLVector Container1a. Write a template func.pdf
Lab 13 Practicing STLVector Container1a. Write a template func.pdf
 
Python for Beginners
Python  for BeginnersPython  for Beginners
Python for Beginners
 
Acm aleppo cpc training ninth session
Acm aleppo cpc training ninth sessionAcm aleppo cpc training ninth session
Acm aleppo cpc training ninth session
 
Presentation 5th
Presentation 5thPresentation 5th
Presentation 5th
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 

More from C++ Homework Help (19)

cpp promo ppt.pptx
cpp promo ppt.pptxcpp promo ppt.pptx
cpp promo ppt.pptx
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
Get Fast C++ Homework Help
Get Fast C++ Homework HelpGet Fast C++ Homework Help
Get Fast C++ Homework Help
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
 
CPP Assignment Help
CPP Assignment HelpCPP Assignment Help
CPP Assignment Help
 
CPP Homework help
CPP Homework helpCPP Homework help
CPP Homework help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Cpp Homework Help
Cpp Homework Help Cpp Homework Help
Cpp Homework Help
 

Recently uploaded

Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 

CPP homework help

  • 1. For any help regarding CPP Homework Help Visit :- https://www.cpphomeworkhelp.com/ , Email :- info@cpphomeworkhelp.com or Call us at :- +1 678 648 4277 CPP Homework Help
  • 2. 1 Additional Material 1.1 Templates and Header Files The compiler does not compile a templated function until it encounters a use of it – until that function is used with a particular type parameter, at which point it is compiled for that type parameter. Because of this, if you define a templated class in a header file and implement its (templated) functions in a .cpp file, the code in the .cpp file will never get compiled unless you use the templated functions within that .cpp file. To solve this problem, templated classes and functions are generally just implemented in the header file, with no .cpp file. 1.2 Templates and friend Functions There are some syntax oddities when using friend functions with templated classes. In order to declare the function as a friend of the class, you need a function prototype (or the full function definition) to appear before the class definition. This is a problem, because you’ll often need to have the class already defined in order for the function prototype to make sense. To get around this issue, when writing friend functions with templated classes, you should include declarations in the following order: Problem CPP Homework Help
  • 3. 1. A templated forward declaration of the class. (A forward declaration is a statement like class SomeClass;, with no class body, that just alerts the compiler that the class definition is coming.) 2. A templated function prototype for the friend function (or the entire function definition). 3. The full templated class definition, including the friend statement. When you write the name of the function in the friend statement, you need to include an extra <> after it to indicate that it is a templated function (e.g., friend MyClass operator+<>(const MyClass &c1, const MyClass &c2;). 4. The operator function definition, if you didn’t include it above. 2 Multi-Type min 2.1 Using templates, implement a min function which returns the minimum of two elements of any comparable type (i.e., it takes two arguments of some type T, and works as long as values of type T can be compared with the < operator). (To test this function, you may need to omit your usual using namespace std; line, since there is already an std::min function.) CPP Homework Help
  • 4. 2.2 Implement the min functionality from part 1 using only preprocessor macros. (Hint: You will probably need the ternary operator – the ?: syntax.) 3 Casting Assume you implemented Problem 5 from Problem Set 3 correctly. This would mean you would have a working Polygon class, and inheriting from that a Triangle class and a Rectangle class. Now imagine you have a pointer declared as Rectangle *rect; that has been properly initialized. 3.1 Write a line of code showing how you would cast rect to a Triangle * without checking for type correctness (i.e., without checking whether it actually points to a Triangle). Do not use C-style casts. 3.2 Now write a line of code that does the same thing, but checks for type correctness and throws an exception or returns a null pointer if rect does not actually point to a Triangle. CPP Homework Help
  • 5. 4 Templated Stack A stack data structure stores a set of items, and allows accessing them via the following operations: • Push – add a new item to the stack • Pop – remove the most recently added item that is still in the stack (i.e. that has not yet been popped) Top – Retrieve For more explanation, see http://en.wikipedia.org/wiki/Stack (data structure). 4.1 Using templates, implement a Stack class that can be used to store items of any type. You do not need to implement any constructors or destructors; the default constructor should be sufficient, and you will not need to use new. Your class should support the following 3 public functions (assuming T is the parameterized type which the Stack is storing): CPP Homework Help
  • 6. • bool Stack::empty() – returns whether the stack is empty • void Stack::push(const T &item) – adds item to the stack • T &Stack::top() – returns a reference to the most-recently-added item • void Stack::pop() – removes the most-recently-added item from the stack Use an STL vector, deque, or list to implement your Stack. You may not use the STL stack class. Make the member functions const where appropriate, and add const/nonconst versions of each function for which it is appropriate. In the case where the Stack is empty, pop should do nothing, and top should behave exactly as normal (this will of course cause an error). When working with templated classes, you cannot separate the function implementations into a separate .cpp file; put all your code in the class definition. (Hint: You can represent pushing by adding to the end of your vector, and popping by removing the last element of the vector. This ensures that the popped item is always the most recently inserted one that has not yet been popped.) 4.2 friend Functions and Operator Overloading (Optional) CPP Homework Help
  • 7. Make a friend function that implements a + operator for Stacks. The behavior of the + operator should be such that when you write a + b, you get a new stack containing a’s items followed by b’s items (assuming a and b are both Stacks), in their original order. Thus, in the following example, the contents of c would be the same as if 1, 2, 3, and 4 had been pushed onto it in that order. (c.top() would therefore return the value 4.) Put your code for this section in the same file as for the previous one. 5 Graph Representation (Optional) A graph is a mathematical data structure consisting of nodes and edges connecting them. To help you visualize it, you can think of a graph as a map of “cities” (nodes) and “roads” (edges) connecting them. In an directed graph, the direction of the edge matters – that is, an edge from A to B is not also an edge from B to A. You can read more at Wikipedia: http://en.wikipedia.org/wiki/Graph (mathematics). CPP Homework Help
  • 8. One way to represent a graph is by assigning each node a unique ID number. Then, for each node ID n, you can store a list of node ID’s to which n has an outgoing edge. This list is called an adjacency list. Write a Graph class that uses STL containers (vectors, maps, etc.) to represent a directed graph. Each node should be represented by a unique integer (an int). Provide the following member functions: • Graph::Graph(const vector &starts, const vector &ends) Constructs a Graph with the given set of edges, where starts and ends represent the ordered list of edges’ start and endpoints. For instance, consider the following graph: The vectors used to initialize a Graph object representing this graph would be: start: 1 1 1 5 5 4 end: 2 3 4 4 2 2 • int Graph::numOutgoing(const int nodeID) const Returns the number of outgoing edges from nodeID – that is, edges with nodeID as the start point • const vector &Graph::adjacent(const int nodeID) const Returns a reference to the list of nodes to which nodeID has outgoing edges CPP Homework Help
  • 9. (Hint: Use the following data type to associate adjacency lists with node ID’s: map >. This will also allow for easy lookup of the adjacency list for a given node ID. Note that the [] operator for maps inserts the item if it isn’t already there.) The constructor should throw an invalid argument exception of the two vectors are not the same length. (This standard exception class is defined in header file stdexcept.) The other member functions should do likewise if the nodeID provided does not actually exist in the graph. CPP Homework Help
  • 10. Solutions 2 Multi-Type min 2.1 2.2 3 Casting 3.1 CPP Homework Help
  • 12. 5 Graph Representation CPP Homework Help