Sie sind auf Seite 1von 5

* While classes define the type of data and functionality that objects will have, instances are "usable"

objects based on the patterns of a particular class.In this model, classes act as collections of behavior (methods) and structure that are the same for all instances, whereas instances carry the object's data * In OOP, an object which is the instance of a class will only have their own variables and pointers to the functions/methods. All the objects(instances) of a particular class will only have thier corresponding methods in a universal way. so inorder to determine which object the method is to act on is acheived by sending a pointer (to the object) to the method and so the method knows which object to act upon and changes the variables by accessing the corresponding offsets from the object's pointer. For Eg: Class A { Private: int a,b; Public: void FirstFunction(int a , int b) // This type of functions or methods are called inline methods because the bodies of inlined methods are copied into the { // code wherever a call to such a method takes place. . It is inline because it is declared and its body return a; // resides inside of the class. It is done for faster execution of code and improves preformance. } void NextFunction(int a , int b); // These type of methods are only declared and resides elsewhere with a scope resol operator and has only one copy in // the whole code. }; void A::NextFunction(int a, int b) // The body of the function protocol of the class A { b = a; // This in compiler terms is changed to this->b = this->a; where this is the pointer to the object for the method to act upon // which will be created by the compiler as an extra arguement to the method's arguement list like // void A::NextFunction(int a , int b, void *this). Same is the case for all the statements of this method and also the previous return b; // method declaration and its statements. } IN BOTH THESE FUNCTIONS/METHODS, THE OBJECT FOR THE METHOD TO ACT UPON IS DECIDED BY SENDING A POINTER TO THE METHOD. WE CAN MANUALLY SPECIFY THAT BUT FORTUNATELY COMPILER AUTOMATICALLY ADJUSTS THE METHOD TO ACCEPT THE POINTER TO OBJECT AND IT ALSO ADJUSTS THE CALLING CODE TO SEND THE POINTER (TO THE CURRENT OBJECT) TO THE METHOD. * Constructors have no return value. As other methods, they can take arguments.

* Destruction of objects takes place to free the space of an object after its use. Destruction of objects take place when the object leaves its scope of definition or is explicitly destroyed. The latter happens, when we dynamically allocate an object and release it when it is no longer needed. An eg for destruction of object when it leaves its scope: Class OBJ {Private: int a; Public: void somefunction(int a){return a;}}; void main() { OBJ AnObject; // initialisation of the object

........ // Some Operations } // Here the destructor of object AnObject is called (Provided by compiler if not explicitly provided) since the scope for AnObject ends here (think of // scope in asm). The destructor also has a pointer to the current object as an arguement ( provided by compiler for both implicit and explicit // destructor). Destructors are declared similar to constructors. Thus, they also use the name prefixed by a tilde (~ ) of the defining class: Eg: Class OBJ { Private: int a; Public: OBJ() { /*Constructor 1 */} // A constructor OBJ(int a) { /*Constructor 2 */} // Another constructor void somefunction(int a){return a;} // A method ~OBJ() { /* NOTHING TO DESTROY */ } // A destructor } Destructors take no arguments. It is even invalid to define one, because destructors are implicitly called at destruction time: You have no chance to specify actual arguments. Destructors dont have return values. - You can have only one destructor for a class OBJ. It's always called OBJ::~OBJ() - A Destructor never takes any parameters, and it never returns anything. - You can't pass parameters to the destructor anyway, since you never explicitly call a destructor (well, almost never). - The destructor will get called again at the close } of the block in which the local was created. This is a guarantee of the language; it happens automagically; there's no way to stop it from happening. But you can get really bad results from calling a destructor on the same object a second time! Bang! You're dead! - You should not explicitly call the destructor, since doing so won't release the memory that was allocated for the AnObject itself. Remember: "delete" does two things: it calls the destructor and it deallocates the memory. - Do not explicitly call the destructor! For early calling of destructors : void someCode() { { File f; ...code that should execute when f is still open... } f's destructor will automagically be called here! ...code that should execute after f is closed... } * In C++, a single colon ":" represents "inherits from" . For Eg: class Point3D : public Point , private AnotherClass {int a;}; // Multiple Inheritance. Point3D inherits from class Point as well as class AnotherClass // class Point3D inherits from Class Point. public means that all members private in the superclass (Point) will remain // private in the subclass (Point3D) and all members declared public will be public in the sub classs. If it was private or // if nothing is stated it will be taken as private (default), then both private and public members of the superclass will be // private in the subclass. If it was protected then private members will be private and public members will be protected. // Protected is used for elements which should be directly usable in subclasses but which should not be accessible from

// the outside. That is Protected is accessible to the current class and all subclasses but not to other classes * Access Rights means the way with which the subclass inherits the superclass. There are three access rights Private, Public and Protected as stated above. Broadly speaking, public means everyone is allowed to access, private means that only members of the same class are allowed to access, and protected means that members of subclasses are also allowed. * Pure Virtual Functions are those wherein only the method declaration(prototype) is provided. A class like this is called ABSTRACT CLASS and a class that inherits from an Abstract class is required to provide the implementation for the prototype specified as virtual in the base class or abstract class. Thus each derived class can have its own implementation of a specific function. A virtual function is a function which has an implementation in the base class also and not just the prototype so that the abstract class can also have the implementation for the corresponding function A virtual function is specified by the keyword "virtual" before the function prototype. In C#, the "virtual" has to be there in the base class and "overrides" in the derived class wishing to have an implementation for the above specified virtual function. An example for a virtual function can be stated like a MathSymbol Class which has pure Virtual function doOperation and derived class Plus will implement that as it sees fit and another derived class Minus will implement it as it sees fit. So implementation for that in the MathSymbol class is not logical and therefore it just gives a prototype and thus becomes and Abstract Class. Format: Class MathSymbol{ virtual int doOperation() ;} Class Plus : public MathSymbol {int doOperation(){/* Some Code */"}} // if C# it should be int overrides doOperation() Class Minus : public MathSymbol {int doOperation(){/* Some Code */"}} // if C# add Overrides NOTE: IT IS A C++ REQUIREMENT TO PROVIDE AT LEAST ONE ROUTINE FOR PURE VIRTUAL FUNCTION AS BACKUP. REMEMBER IN KERNEL DEV. * Classes that contain pure virtual functions are called Abstract classes. Their virtual methods are designated to be necessarily defined by subclasses. In c++ it is declared by the prototype = 0; like class DrawableObject {public: virtual void print() = 0;}; They are ordinary classes and they may have non-virtual methods. This class definition would force every derived class from which objects should be created to define a method print(). These method declarations are also called pure methods. Pure methods must also be declared virtual, because we only want to use objects from derived classes. Classes which define pure methods are called abstract classes. * Operator "new" creates a new object of the specified type in dynamic memory and returns a pointer to it. Similar to malloc and then crating object there and then returning its pointer. Eg: class Colour { public: virtual ~Colour(); }; class Red : public Colour {public: ~Red(); // Virtuality inherited from Colour}; class LightRed : public Red {public: ~LightRed();}; Using these classes, we can define a palette as follows: Colour *palette[3]; palette[0] = new Red; // Dynamically create a new Red object.The elements of palette are pointers to Colour and, because Red is-a Colour the assignment is valid. palette[1] = new LightRed; // similarly palette[2] = new Colour;

* Operator "delete" explicitly destroys an object referenced by the provided pointer and reclaims the memory. Eg: delete palette[0]; // Call destructor ~Red() followed by ~Colour() delete palette[1]; // Call ~LightRed(), ~Red() and ~Colour() delete palette[2]; // Call ~Colour() The various destructor calls only happen, because of the use of virtual destructors. If we would have not declared them virtual, each delete would have only called ~ Colour() (because palette[i] is of type pointer to Colour). * We can define functions or classes to be friends of a class to allow them direct access to its private data members.You should not use friends very often because they break the data hiding principle in its fundamentals. If you have to use friends very often it is always a sign that it is time to restructure your inheritance graph. "friend" keyword should be specified inside the class so that another class with the prototyped declaration can be a friend and access its private data. Eg: Class A{ friend void B(int & , int ); }; // Now B can be a friend of A if B is implemented as void B(int &a , int b){ / * some code */} * If an object is created without the "new" keyword, then the contents of that object will be placed on the stack. If it is created with the "new" keyword, then the object is placed on the heap returned by the Operating system as part of the dynamic memory allocation and mapped to the page directory of the corresponding process / thread. It not good to place too much on the stack. For a class named QLabel, if we create an object ql out of it like QLabel ql; it creates a QLabel on the stack. Note that it just lives until the current scope exits. Scope means the } ( or labels in asm). NOTE: It's only created on the stack if this code is in a function. As a global it will be created on the heap. * The point is that extern "C" is the closest thing there is to a cross-platform way of telling the compiler to make the function use the standard C calling convention on that platform (i.e. exactly how parameters and return values should be passed on the stack). It usually stands for cdecl calling convention in c++ * ... means unknown or variable number of arguements, etc

* Memory leaks means not properly destructing the objects like destructing a parent object without a chiled object. In this particular case you can just add virtual destructor to the parent class so that the child class's destructor will also get called once you destruct the parent class. This is one of the causes of memory leak i.e allocating by way of "new" an object on the heap and not releasing it once its use or scope is over. * Because the type of the object is saved the compiler will know where to find the methods that are being called on the object. So even though a new copy of the data is created when a new object is made, the methods still are fixed. This may not be applicable for inline methods and other aspects like compiler optimisation turned on or off etc.. * Generics and Templates are the same. In C# it is called "parameterized types". They

allow classes to be reused with different datatypes. It is just a simple way to write same code for different data types which the compiler will generate different codes as specified by the caller's datatype. Generics means general i.e its general code for all datatypes for a particular functionality. It is implemented only to reduce the code written by the prorammers as the compiler will do that kind of stuff. For eg: Template <Class T> T Example(T a) { return T; } main() { int a = 3 ; string str = "Abc"; Example(a); // will substitute the "int" for T in the Example function and the compiler will generate such a code for Example with "int" datatype Example(str); // will substitute "string" for T in th Example Function and the compiler will generate another code for Example with "string" datatype }

Das könnte Ihnen auch gefallen