Showing posts with label object oriented. Show all posts
Showing posts with label object oriented. Show all posts
Feb 5, 2009
Add a function in an existing class, if the class does not have virtual function, and you add a virtual function in the class, you should recompile th
Add a function in an existing class, if the class does not have virtual function, and you add a virtual function in the class, you should recompile the class. Since for each instance of the class, a virtual pointer should be added.
Feb 4, 2009
protected access
public class A
{
protected virtual void Foo()
{
Console.WriteLine("A.Foo");
}
}
public class B : A
{
public void CallMethod(A a)
{
a.Foo();
}
}
.........
static void Main()
{
B b = new B();
b.CallMethod(new A());
}
B can access the protected member of A which it inherits from i.e. A::Foo() or simply Foo() because that is a member of B now and it has access to that but not a.Foo() because for B "a" is just another instance of a class and one cannot access the protected members of a variable you create.
{
protected virtual void Foo()
{
Console.WriteLine("A.Foo");
}
}
public class B : A
{
public void CallMethod(A a)
{
a.Foo();
}
}
.........
static void Main()
{
B b = new B();
b.CallMethod(new A());
}
B can access the protected member of A which it inherits from i.e. A::Foo() or simply Foo() because that is a member of B now and it has access to that but not a.Foo() because for B "a" is just another instance of a class and one cannot access the protected members of a variable you create.
Jan 22, 2009
Pure virtual function can has body
Pure virtual function can has body
A pure virtual function is a virtual function that you want to force derived classes to override. If a class has any unoverridden pure virtuals, it is an "abstract class" and you can't create objects of that type
Pure virtual destructor should have body: if there is no body, the child class cannot be instantiated, because when destroy the child’s object, which call base class destructor. The reason declaring pure keyword is that we want to prevent base class being instantiated (no other pure virtual function in base class).
http://www.gotw.ca/gotw/031.htm
A pure virtual function is a virtual function that you want to force derived classes to override. If a class has any unoverridden pure virtuals, it is an "abstract class" and you can't create objects of that type
Pure virtual destructor should have body: if there is no body, the child class cannot be instantiated, because when destroy the child’s object, which call base class destructor. The reason declaring pure keyword is that we want to prevent base class being instantiated (no other pure virtual function in base class).
http://www.gotw.ca/gotw/031.htm
multi inheritence
class A
{
int x;
};
class B
{
int y;
};
class C : public A, public B
{
int z;
};
void main()
{
A *aa;
B *bb;
C cc;
aa = &cc;
bb = &cc;
cout << &cc << endl;
cout << aa << endl;
cout << bb << endl;
}
aa and bb do not have the same address, due to point alignment
http://www.newlc.com/Multiple-Inheritance.html
{
int x;
};
class B
{
int y;
};
class C : public A, public B
{
int z;
};
void main()
{
A *aa;
B *bb;
C cc;
aa = &cc;
bb = &cc;
cout << &cc << endl;
cout << aa << endl;
cout << bb << endl;
}
aa and bb do not have the same address, due to point alignment
http://www.newlc.com/Multiple-Inheritance.html
How does the compiler know which virtual table to point to when the base class pointer points to its derided class object
How does the compiler know which virtual table to point to when the base class pointer points to its derided class object
http://www.learncpp.com/cpp-tutorial/125-the-virtual-table/
http://www.learncpp.com/cpp-tutorial/125-the-virtual-table/
If it is a member function which create a temp variable such as assignment operator, just return by value, not return by reference because it is a loc
If it is a member function which create a temp variable such as assignment operator, just return by value, not return by reference because it is a local object or dynamic allocated object…
If it is a copy constructor, no return value at all
If it is a assignment operator, return value is a reference to *this
If it is a copy constructor, no return value at all
If it is a assignment operator, return value is a reference to *this
Jan 21, 2009
The member function of derived class will hide the member function of base class if they have the same function name with different signature, not ove
The member function of derived class will hide the member function of base class if they have the same function name with different signature, not overloading. If you want to use overloading, declare “using base class::mf;”
http://publib.boulder.ibm.com/infocenter/systems/index.jsp?topic=/com.ibm.xlcpp9.cell.doc/language_ref/overload_member_fn_base_derived.htm
http://publib.boulder.ibm.com/infocenter/systems/index.jsp?topic=/com.ibm.xlcpp9.cell.doc/language_ref/overload_member_fn_base_derived.htm
Overloading is compiling time polymorphism, overridden is run time polymorphism
Overloading is compiling time polymorphism, overridden is run time polymorphism
Initialization list:
Initialization list:
Child class always calls parent class’s default constructor, if you do not want to call parent class’s default constructor, use initialization list to specify parent class’s constructor with parameters.
Child class always calls parent class’s default constructor, if you do not want to call parent class’s default constructor, use initialization list to specify parent class’s constructor with parameters.
In C++ and Java, overloaded functions(methods) must not differ just by the return type. Is it necessary or by design? What is the reason?
In C++ and Java, overloaded functions(methods) must not differ just by the return type. Is it necessary or by design? What is the reason?
It is necessary(No idea about java but in C++ it's so). Because the call to a function is bound to the function definition only by its signature i.e. its name, no. of parameters, type of parameters (and other factors like virtual function come into picture for dynamic binding but not the return value for sure).
However you can overload return type only with const keyword. When you return a reference and you do not want the reference changed in program, you should company the reference with a const.
It is necessary(No idea about java but in C++ it's so). Because the call to a function is bound to the function definition only by its signature i.e. its name, no. of parameters, type of parameters (and other factors like virtual function come into picture for dynamic binding but not the return value for sure).
However you can overload return type only with const keyword. When you return a reference and you do not want the reference changed in program, you should company the reference with a const.
In C++ and Java, if a derived class does not explicitly invoke a constructor of the base class (in C++ in the initialize list, in Java using super() i
In C++ and Java, if a derived class does not explicitly invoke a constructor of the base class (in C++ in the initialize list, in Java using super() in the first line), then the default constructor is implicitly invoked for the base class. If the base class does not have a default constructor, it is an error.
C++ inheritance Derived class does not inherited from base class including
C++ inheritance Derived class does not inherited from base class including
constructor and destructor
assignment operator
friend class and friend function
constructor and destructor
assignment operator
friend class and friend function
Base class’s destructor is virtual, if the destructor is not virtual, derived class’s destructor is not called.
Base class’s destructor is virtual, if the destructor is not virtual, derived class’s destructor is not called.
Calling order: base class’s constructor-> derived class’s constructor
Calling order: derived class’s destructor->base class’s destructor
Calling order: base class’s constructor-> derived class’s constructor
Calling order: derived class’s destructor->base class’s destructor
Template vs. Inheritance. Why use one over the other?
Template vs. Inheritance. Why use one over the other?
A template should be used to generate a collection of classes when the type of the objects does not affect the behavior of the class's functions.
Inheritance should be used for a collection of classes when the type of the objects does affect the behavior of the class's functions.
A template should be used to generate a collection of classes when the type of the objects does not affect the behavior of the class's functions.
Inheritance should be used for a collection of classes when the type of the objects does affect the behavior of the class's functions.
Why would you use an interface over an abstract class?
Why would you use an interface over an abstract class?
java doesn't allow multiple inheritance. that's where interfaces come in.
but you cannot implement any method inside an interface. so if u need to implement some methods and leave others with only the declaration(prototype) better use abstract classes.
java doesn't allow multiple inheritance. that's where interfaces come in.
but you cannot implement any method inside an interface. so if u need to implement some methods and leave others with only the declaration(prototype) better use abstract classes.
What is virtual class?
What is virtual class?
A virtual class is an inner class that can be overridden by derived classes of the outer class
A virtual class is an inner class that can be overridden by derived classes of the outer class
Discussion on virtual inheritance
Discussion on virtual inheritance
in multiple inheritance, if a class A derived from two classes B and C which derived from the same based class D, and if the class A want to call a member function which is defined in the base class D, there should be a ambiguity with the function call. So when you derive class B and C from base class D, you should use virtual inheritance to resolve this problem.
in multiple inheritance, if a class A derived from two classes B and C which derived from the same based class D, and if the class A want to call a member function which is defined in the base class D, there should be a ambiguity with the function call. So when you derive class B and C from base class D, you should use virtual inheritance to resolve this problem.
What is the effect by keeping a constructor private? (in terms of inheritance)
What is the effect by keeping a constructor private? (in terms of inheritance)
You cannot derive this class
You cannot derive this class
Difference between deep copy & shallow copy
Difference between deep copy & shallow copy
A shallow copy (bitwise copy) of an object copies all of the member field values. This works well if the fields are values, but may not be what you want for fields that point to dynamically allocated memory. The pointer will be copied. But the memory it points to will not be copied
A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. To make a deep copy, you must write a copy constructor and overload the assignment operator; otherwise the copy will point to the original, with disastrous consequences.
A shallow copy (bitwise copy) of an object copies all of the member field values. This works well if the fields are values, but may not be what you want for fields that point to dynamically allocated memory. The pointer will be copied. But the memory it points to will not be copied
A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. To make a deep copy, you must write a copy constructor and overload the assignment operator; otherwise the copy will point to the original, with disastrous consequences.
Jan 18, 2009
copy constructor and assignment operator
class A
{
public:
int i;
A(int ii) { i = ii; }
A(const A& a) { i = a.i; i++; }
A& operator=(const A& a) { this->i = a.i; i--; return *this}
};
int main(void)
{
A a(4);
A b = a; //this calls copy constructor not assignment operator
//A b(4); b=a; this calls assignment operator
cout << b.i << endl;//the result is 5
return 0;
}
{
public:
int i;
A(int ii) { i = ii; }
A(const A& a) { i = a.i; i++; }
A& operator=(const A& a) { this->i = a.i; i--; return *this}
};
int main(void)
{
A a(4);
A b = a; //this calls copy constructor not assignment operator
//A b(4); b=a; this calls assignment operator
cout << b.i << endl;//the result is 5
return 0;
}
Subscribe to:
Posts (Atom)