Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ Containers Cheat Sheet, Cheat Sheet of Computer Programming

C++ language structures, includes, private, public and friend classes cheat sheet. Babes-Bolyai University computer science document

Typology: Cheat Sheet

2020/2021

Uploaded on 04/26/2021

alfred67
alfred67 🇺🇸

4.9

(20)

75 documents

Partial preview of the text

Download C++ Containers Cheat Sheet and more Cheat Sheet Computer Programming in PDF only on Docsity! C++ Cheat Sheet You can find below information that can help with the implementation of your project. Some of them might be familiar from your Object Oriented Programming course as well. 1. Structure In C++ code is divided into two types of files: header files (with the extension .h) and code files (with the extension .cpp). In the .h file we have the declarations while the actual implementation goes in the .cpp file. Normally, for the ADT implementation your project should contain the following files: - Container.h (obviously, it is not going to be called container, you will use the name of the exact container you have to implement, for example Bag.h, Map.h, etc.) o This is where you have the declaration of your container class, together with its attributes and methods. - Container.cpp o This is where you have the implementation of the operations enumerated in the container class declaration. - Iterator.h (if you have ADT PriorityQueue or Matrix, you do not have an Iterator) o This is where you have the declaration of your iterator class, together with its attributes and methods - Iterator.cpp o This is where you have the implementation of the operations enumerated in the iterator class declaration. 2. Includes The next section is valid only for those projects, where there is an iterator class as well. Normally, Container.h should have an include for Iterator.h, because your container class will have an operation called iterator which returns an element of type Iterator. However, Iterator.h should also include Container.h, because you will keep a variable of type container as an attribute in the Iterator. If you have both includes, it will lead to a circular dependency and a lot of compile time errors. The solution for this problem is the following: You will include the Container.h file in the Iterator.h, but you will NOT include the Iterator.h file in the Container.h file. This leads to a compile error in Container.h, where the compiler complains that it does not know the Iterator type returned by the iterator operation. To avoid this, you should add a forward declaration to class Iterator. This means that before the container class, you will simply write: class Iterator; without any further implementation for the class (obviously it will be implemented in the Iterator.h file). This forward declaration is enough for C++ to know that there will be a class called Iterator. Obs.: in the .cpp files however you can include everything circular dependencies are not a problem in those files. 3. Private/Public The representation (attributes) of your container and iterator MUST be private. It must be hidden. The operations of both classes are public, but only those operations that exist in the interface of the ADT (the operations you are going to put in the project stage). You are allowed to add other operations in the container and/or iterator, but they must be private. They can be helper functions used for implementing the other ones, but they are not allowed to be visible from outside of the class. More precisely, you are not allowed to have getters and setters for your attributes. Representation is hidden (private) and this means that you are not allowed to return them! 4. Friend classes The representation of your container and iterator will be private. But the iterator needs access to the representation of the container, in order to access the elements. But you are not allowed to make the representation public. The solution for such situations is to use the friend class mechanism. If in your container class you add friend class Iterator;
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved