answersLogoWhite

0

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:

  • You don't use the term "#include". Instead, you use "import". The reason being that Java is a managed language, and is not native to the operating system. Java has an extensive number of libraries given to the programmer, and rather than having to get an external library like in C++, importing from what is given will suit most circumstances
  • Class variables are not put in a section. Instead, if you want to label a variable private, you merely write private before it's declaration: for example private int hello;
  • Variables can be given a value as they're declared

An example Java class:

class foobar

{

private int hello = 0;

private boolean exists = false;

public foobar()

{

//constructor of the class

}

}

User Avatar

Wiki User

∙ 14y ago
User Avatar

What else can I help you with?

Related Questions

How can you create a class which cannot be inheritable?

Declare it final.


When do you declare a method or class final?

if we declare a method as final then it can be changed.


How do you prevent extending a class in java?

You can declare a class as "final".


How do you prevent class extending in java?

Declare the class as final. final class A{ ... }


When will be the result of tenth class declare?

no one is sure about it thwt when it is going to declare


The Java keyword is used to declare a class as a subclass of another class?

class MyClass extends AnotherClass {}


What is the relationship between inheritance methods and the keyword final?

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 do you declare a method or class abstract in java?

when overriding of a class or a method is necessary, they can be declared as abstract


How many static function can declare in one class?

It is dependent on the requirement of your usage of the Object of that class


How you declare class scoped variables and member functions?

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.


When do you declare a class as final?

By using the final keyword in the class declaration statement. Ex: public final class Test {...}


What happen if you declare class as recursive?

It will get a compiler error in Java.