C++
Within your project, create a .hpp file. By this, I mean, using the name of class, create a new class.hpp file. A .hpp file is a header file, and contains a classes information.
So, if your class is called foobar, your .hpp file will be called foobar.hpp.
Within your new .hpp file, create a class in this manner:
FOOBAR.HPP
class foobar
{
public:
foobar();
~foobar();
private:
int variable;
}
This is the standard layout of a class, and every class you will create will look something like this.
There are two sections within the class that you'll notice; public and private. As the names suggest, everything within the public section can be accessed by anything, where as private data or functions can only be accessed within the class itself.
You'll notice some functions with the same name as your class name. foobar() is the class constructor. This function is called when the class is instantiated. Set up any class variables in here.
Second of all, you'll notice ~foobar();. This is the class destructor. This function is called when the class is destroyed. If you have dynamically allocated any memory, here is the place to delete it.
Once you've done this, create a corresponding .cpp file. So, in continuation, create a file called foobar.cpp.
Within this file, you'll need to implement the functions we've created in the .hpp:
FOOBAR.CPP
#include "foobar.hpp"
foobar::foobar()
{
//do something
}
foobar::~foobar()
{
//do something else
}
Within the .cpp file, we have included the .hpp file. The inclusion means, if any functions are used from the class, and if they're labelled properly (with a preficed foobar::) the compiler knows which functions is being referred to.
JAVA
In Java, classes are created in a similar way. The main differences are as follows:
An example Java class:
class foobar
{
private int hello = 0;
private boolean exists = false;
public foobar()
{
//constructor of the class
}
}
class MyClass extends AnotherClass {}
They are inversely related. That is: If you declare a method as final you cannot overridden in the child class If you declare a class as final you cannot inherit it in any other class.
when overriding of a class or a method is necessary, they can be declared as abstract
To scope class members to the class (rather than to instances of the class), declare them as static members of the class. Static members are accessible even when no instances of the class exist. As such, static member functions do not have access to a 'this' pointer, unlike ordinary (nonstatic) member functions.
Yes, you can declare and define the constructor within a class. A constructor is a special member function of a class that is automatically called when an object of the class is created. It is used to initialize the object's data members. The constructor can be declared and defined within the class definition or can be defined outside the class definition using the scope resolution operator (::).
Declare it final.
if we declare a method as final then it can be changed.
You can declare a class as "final".
Declare the class as final. final class A{ ... }
no one is sure about it thwt when it is going to declare
class MyClass extends AnotherClass {}
They are inversely related. That is: If you declare a method as final you cannot overridden in the child class If you declare a class as final you cannot inherit it in any other class.
when overriding of a class or a method is necessary, they can be declared as abstract
It is dependent on the requirement of your usage of the Object of that class
To scope class members to the class (rather than to instances of the class), declare them as static members of the class. Static members are accessible even when no instances of the class exist. As such, static member functions do not have access to a 'this' pointer, unlike ordinary (nonstatic) member functions.
By using the final keyword in the class declaration statement. Ex: public final class Test {...}
It will get a compiler error in Java.