Hashim Nangarhari
asked on
C++ code explanation request
Hello C++ experts!
Kindly explain this code in detail:
Class Queue {
Public:
Queue();
Queue(const Queue&);
Virtual Queue();
Virtual Queue& operator=(const Queue&);
Virtual int operator==(const Queue&) const;
Int operator!=(const Queue&) const;
Virtual void clear();
Virtual void append(const void*)
Virtual void pop();
Virtual void remove(int at);
Virtual int length() const;
Virtual int isEmpty() const;
Virtual const void* front() const;
Virtual int location(const void*);
Protected:
……
};
Kindly or not kindly, you should start with some simpler tutorial.
@Hashim Nangarhari
As per the comment from pepr:
try learning from tutorial.
Example:
https://www.w3schools.com/CPP/default.asp
Try it yourself:
https://www.w3schools.com/CPP/trycpp.asp?filename=demo_helloworld
As per the comment from pepr:
try learning from tutorial.
Example:
https://www.w3schools.com/CPP/default.asp
Try it yourself:
https://www.w3schools.com/CPP/trycpp.asp?filename=demo_helloworld
Error from your code:
Class => class
Public => public
Virtual => virtual
no destructor
Int => int
Protected=> protected
functions not defined
virtual destructor not defined.
Class => class
Public => public
Virtual => virtual
no destructor
Int => int
Protected=> protected
functions not defined
virtual destructor not defined.
ASKER
Hi MURUGESAN N
the code is not complete I know, I am trying to understand the syntax for example here:
virtual int operator==(const Queue&) const;
int operator!=(const Queue&) const;
what the const keyword is used for ?
what the const keyword at the end is used for ?
the code is not complete I know, I am trying to understand the syntax for example here:
virtual int operator==(const Queue&) const;
int operator!=(const Queue&) const;
what the const keyword is used for ?
what the const keyword at the end is used for ?
const at the tail here means that the object is not changed in any way by the operation. Since both are comparison operators, this sounds superfluous, but the compiler doesn't know that ;-).
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
The virtual near the method prototype means that if the method is defined by the derived class, its code (from the derived class) will be used even when the base class call the method. (The compiler creates a table of references to virtual functions, and the method is called indirectly through that table. The derived class causes to fill that method reference later.)
If the method is not virtual, it can still be redefined by derived classes; however, each class (base or derived) have its own code for the method, and the programmer can even from the derived class choose whether the code from the derived class should be used (natural, simple syntax), or whether the code of the base-class method should be called (more complex syntax, explicitly naming the base class as the part of the call).
It depends on situation whether you want to define the method as virtual or not. One of the major reasons for using virtual is to implement polymorphic behavior.
If the method is not virtual, it can still be redefined by derived classes; however, each class (base or derived) have its own code for the method, and the programmer can even from the derived class choose whether the code from the derived class should be used (natural, simple syntax), or whether the code of the base-class method should be called (more complex syntax, explicitly naming the base class as the part of the call).
It depends on situation whether you want to define the method as virtual or not. One of the major reasons for using virtual is to implement polymorphic behavior.
A queue allows to add content at the tail and remove at the head (for processing) - just like any queue in real life.