Notas
C++ Notes
Conteúdo das notas (5)
Página 1
1. In copy contructor, parameter passed must always having &. (that is it is passed by refernce).
2. why static methods cannot be const?
Answer :
first of all, what is a const function - A "const function", denoted with the keyword const after a function declaration, makes it a compiler error for this class function to change a member variable of the class. However, reading of a class variables is ok inside of the function, but writing inside of this function will generate a compiler error. Another way of thinking about such "const function" is by viewing an class function as a normal function taking an implicit this pointer. So a method int Foo::Bar(int random_arg) (without the const at the end) results in a function like int Foo_Bar(Foo* this, int random_arg), and a call such as Foo f; f.Bar(4) will internally correspond to something like Foo f; Foo_Bar(&f, 4). Now adding the const at the end (int Foo::Bar(int random_arg) const) can then be understood as a declaration with a const this pointer: int Foo_Bar(const Foo* this, int random_arg). Since the type of this in such case is const, no modifications of member variables are possible. It is possible to loosen the "const function" restriction of not allowing the function to write to any variable of a class. To allow some of the variables to be writable even when the function is marked as a "const function", these class variables are marked with the keyword mutable. Thus, if a class variable is marked as mutable, and a "const function" writes to this variable then the code will compile cleanly and the variable is possible to change.
When you apply const qualifier to the non static member function, this affects the 'this' pointer. the type of this pointer will be c const*. if not const qualified, the the this pointer type will be c* . a Static member function does not have this pointer, so const qualification of static member function doesn make sense.
3. why static functions cannot be virtual?
Answer : A virtual function cannot be global or static, because virtual function is a base class member function and relies on the object to determine which implementation of the function is called.
4. why static functions cannot be overridden?
https://www.quora.com/Why-cant-static-methods-be-overriddenhttps://www.quora.com/Why-cant-static-methods-be-overridden
Answer : cannot be overriden because override happens dynamically, where as static is a static lookup method (compile time).
Static is accessed directly by the class, object creation is not needed. Static methods are not part of objects state. Rather they belolng to the class.
5. what is implements.?
Answer : Implements is a concept of java, and not used in c++. the same can be achieved through abstract classes and pure virtual functions.
6. Can static method be overloaded?
Answer : Yes. Static method can be overloaded. Overloading happens within a class (unlike overriding which happens with differnt classes). for static, instances are not needed, one class definition is needed. if you have the definition correct, you can have the static function overloaded anytime. No matter how many instances u have, you will have only one copy of the class methods, whether static or not.
We can override main() function inside a class, but not outside.
Página 2
DATA STRUCTURES
What are data structures ?
Data Structure are used to store data in computer in organised fashion.
It is a scheme of organizing related information.
Data Structures are of two types :
Primitive data Structures
integer
float
char
pointers
Non Primitive Data Structures
Linear
Arrays
Linked Lists
Stacks
Queues
Non Linear
Trees
Graphs
Tables
Sets
Stack works in First in Last out (FILO) order. Element added t in the stack will be removed at the end.
Queue works in first in First out (FIFO) order. Elements inserted first will be removed first.
Linked LIst stores data in Linear fashion.
Trees stores data in non linear fashion, with one root node and sub nodes.
[Records and tables]
Página 3
STANDARD TEMPLATE LIBRARY
STL has 4 main components :
Container
Iterators
Algorithms
Functions
Containers :
Containers or container classes, store objects and data.
There are 7 'first class' container classes and 3 container adaptor. And only 7 header files provide access to these containers or container adaptors.
Different types of containers :
Sequence container : Implement data Structure that can be accessed in sequential order.
vector
list
deque
arrays
forward_list (c++11)
Container adaptors : provide differnt interface for sequential containers
queue
stacks
priority_queue
Associative Containers : implement sorted data structures that can be quickly searched.
set
multiset
map
multimap
Algorithm :
Algorithm included sorting, searching, reversing, max element, accumulate(insert)
New array algorithms are included c++ 11 - such as all_of() , any_of(), copy_n() etc. ( https://www.geeksforgeeks.org/useful-array-algorithms-in-c-stl/ )
Function :
STL includes classes that overload the function call operator. Instances of such classes are called function objects or functors.
Functors allows the working of associated functions to be customised with the help of parameters to be passed.
Iterators :
Iterators are used to work upon a sequence of values.
Página 4
DESIGN PATTERNS
Design pattern is a general solution for commonly occurring problem in software design. The patterns typicall y shows the relationship and interaction between the objects and classes. Idea is to speed up the development process by providing tested, proven development paradigm.
Types of Design Patterns :
Creational
Its all about class instantiation and object creation. They can be further classfied into 'class-creational patterns' and 'object-creational' patterns. While class creational patters use inheritance, object creational patterns use delegations(assignments).
Creational Design Patterns are Factory Method, Abstract Method, Builder, Singleton, Object Pool and Prototype.
Structural
Structural design patterns are about organising different classes and object to form and new structure and provide new functionalities.
They are Adapters( allows you to repurpose a class with different interface, allowing it to be used by the system that uses different calling method. Another term is Wrapper.[it is basically same as saying : instead of assigning values to member variables again and again in different places, we can create a class and have a basic function for value assignment, and this function can be called in places as per need.]) , Bridge, Composite, Decorator, Facade, Flyweight, Private Data Class and Proxy.
Behavioral
These are observing communication pattern between objects and realize these patterns.
They are Chain of responsibility, Command, Interpretor, Iterator, mediator, Momento, Null Object, Observer, State, Strategy ( which course you should take during a specific context during runtime), Template Method, Visitor.
What are S.O.L.I.D Design Priciples?
These are 5 design principles, when combined together, it make it easier for the programmer to develop a software that are easy to maintain and extend.
1. Single Responsibility Principle.
2. Open Closed Principle.
3. Liskov Substitution Principle.
4. Interface Segregation Principle.
5. Dependency Inversion Principle.
Single Responsibility Principle :
SRP - short form .
Principle states that : A class should have one and only one reason to change, meaning that the the class should have only one job.
This can be explained using a simple example, where we have AreaCalculator class to calculate the sum of the area of all provided shapes. This class would also have taken care of the output part. But if the user wants to output the data in Json, then the logic will have to be changed again. Therefore, AreaCalculator class should only sum the areas of the provided classes and should not bother about the kind of outpupt the user wants. To fix this we can create a seperate class SumCalcOutputter, that will bother about the kind of output the user wants.
Open Closed Principle :
Principle states the the object or entity should be open for extension, but closed for modification.
This simply means that the class should be easily extendable without modifying the class.
Eg : the AreaCalculator class which was mentioned earlier has a list of classes whose sum is calculated. If you want to calculate the sum of few more shapes, you will have to change the logic accordingly. This modification should not be made as per the principle. So instead of calculating the area of each shape class, in Sum method of AreaCalculator, we can remove the logic to calculate area in Sum method and attach the logic to the shape's class itself. So as and when new shapes are designed, sum can be calculated accordingly. To confirm that every shape class has an function area calculated, we can have an interface having the function Area, so that every Shape class has to define of its own. [ AreaCalculator is sum of area] Thus interface is an integral part of SOLID Principles.
Liskov Principle :
The principle states that : Let q(x) be a property provable for object x of type T. Then q(y) should be provable for object y of type S, where S is a subtype of T.
This states tat every subclass/derived class should be substitutable for the base/parent class.
Interface Segregation Principle :
The principle says that : A client should never be forced to implement an interface it doesn use or clients shouldnt be forced to use methods they do not use.
for Example, if we want to add volume functions we can add it to ShapeInterface. But squares are flat shaped and they do not have volume. Thus we cannot have the class of square to have volume defined for it when there is no use of the same.
Instead, we can create another interface called SolidShapeInterface, that has Volume contract and shapes like cube can implement this.
Dependency Inversion Principle :
This states that : the entities must depend on abstraction not on concretions. It states that the high level module should not depend on low level module, but they should depend on abstraction.
Eg : MySQLConnection is a low level module and PasswordReminder is a high level modules. But according to principle, there is a voilation because PasswordReminder is dependent MySQLConnection. (if change in connection type, again password will have to be entered). For this we can create an interface instead of calling it directly in PasswordReminder. The interface has a connect method and the MySQLConnection class implements this interface, also instead of directly type-hinting MySQLConnection class in the constructor of the PasswordReminder, we instead type-hint the interface and no matter the type of database your application uses, the PasswordReminder class can easily connect to the database without any problems and OCP is not violated.
Página 5
Templates
Templates allows you to write generic programs. You can create a single function or class, to work with differnt data types using templates.
-code reusability and flixibility.
Concept of templates can be in two different ways :
Function Templates
Class Templates.
Function Templates :
This works like any other normal function , with one key difference that is a single function template can work with different data types. Normally we can use function overloading, but a netter approach would be using template functions because you can perform the same task writing less and maintainable code.
Declare a function Template :
template <class T>
T somefunction (T arg)
{
....
}
In the above code, T is the template argument that accepts different data types and class is a keyword. We can also use typename instead of class.
When an argument of some data type is passes to someFunction(), compiler generates a new version of some function() for the given data type.
Example :
Informações
- Descrição
- Notas sobre C++ Notes, criado por Lavanya A em 04-10-2018.
- Criado por
- Lavanya A · out 2018
- Atualizado
- 3 dez 2018
- Visualizações
- 5
- Avaliação