Saturday, January 21, 2006

C++ vs. Java

- Java does not have pointer but instead has non primative variables that references
- Java avoids much of the direct programmer management of memory that causes so many bugs in C and C++
- Java does not have arrays which are references
- Java does not have functions that are outside the scope of a class
- Java's term for function is method
- Multiple inheritance and concept of virtual inheritance in C++. No multiple inheritance in Java. Can get around by user interface
- By default member of class are privates where Java a default member function/variables are visibility
- Platform independence in Java which is not in C++
- All member function in Java class are virtual by default, where C++ need specified
- Constructor can be called from other constructors using this() in Java, this is not in C++

Thursday, January 19, 2006

What is the Virtual Function ?

A virtual function is a member of base class that can be overridden by a derived class. If the virtual function is not overridden by the derived class, the base class definition is used.

// virtual members
#include
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area (void)
{ return (0); }
};
class CRectangle: public CPolygon {
public:
int area (void)
{ return (width * height); }
};
class CTriangle: public CPolygon {
public:
int area (void)
{ return (width * height / 2); }
};
int main () {
CRectangle rect;
CTriangle trgl;
CPolygon poly;
CPolygon * ppoly1 = ▭
CPolygon * ppoly2 = &trgl;
CPolygon * ppoly3 = &poly;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
ppoly3->set_values (4,5);
cout <<>area() <<>area() <<>area() << endl; return 0; }
References:
http://people.msoe.edu/~tritt/clang.html#toc
http://www.cplusplus.com/

What is Polymorphism ?

Polymorphism is the capability to bind a specific derived class object to base class pointer at run-time.

what is the structure ?

C++ language has extended the C keyword struct to the same functionality of the C++ class keyword except that its members are public by default instead of being private.
Anyway, due to that both class and struct have almost the same functionality in C++, struct is usually used for data-only structures and class for classes that have procedures and member functions.
struct model_name {
type1 element1;
type2 element2;
type3 element3;
.
.
} object_name;

What is Class ?

A class is a blue-print or prototype defines the variables, functions/methods for an object.
class class_name {
permission_label_1:
member1;
permission_label_2:
member2;
...
} object_name;

What is object ?

An object is a software bundle of related variables and methods. Software objects are often used to model real world objects that we found in everyday life.
A class is the blueprint from which individual objects are created.
An object is an individual instance of a class.