Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why write a "class" in the declaration of a class object?

Tags:

c++

oop

I don't usually write this way but I've seen one code base where it's almost everywhere, e.g:

class prettyClass
{
    // just class stuff
};

int main()
{
    class prettyClass obj; // class?
    return 0;
}
like image 579
ClownieAdd Avatar asked Jun 25 '21 11:06

ClownieAdd


People also ask

What happens when an object of a class is declared?

Declaring Objects (Also called instantiating a class) But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances. As we declare variables like (type name;). This notifies the compiler that we will use the name to refer to data whose type is type.

Why do we declare a class in Java?

Classes are a blueprint for creating individual objects that contain the general characteristics of a defined object type. A modifier may or may not be used to declare a class.

What is the purpose of a class in object-oriented programming?

In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).

What does it mean to declare a class?

The class declaration component declares the name of the class along with other attributes such as the class's superclass, and whether the class is public, final, or abstract.

What are the components of a class declaration?

In general, class declarations can include these components, in order: Modifiers: A class can be public or has default access (Refer this for details). class keyword: class keyword is used to create a class. Class name: The name should begin with an initial letter (capitalized by convention).

What is the purpose of a class in Java?

Creating an Object As mentioned previously, a class provides the blueprints for objects. So basically, an object is created from a class. In Java, the new keyword is used to create new objects.

How do you declare a class in C++ with example?

Declaration and Definition of Class in C++ You can define classes using the keyword ‘class’ followed by the name of the class. Here, inside the class, there are access-modifiers, data variables, and member functions. Now, understand them in detail.

How do you create an object from a class?

So basically, an object is created from a class. In Java, the new keyword is used to create new objects. Declaration − A variable declaration with a variable name with an object type. Instantiation − The 'new' keyword is used to create the object. Initialization − The 'new' keyword is followed by a call to a constructor.


1 Answers

In C, the tags of structures live in a different namespace to other identifiers. So unless one introduces an alias, they must write struct T for the type name. In C++ (where class and struct are of the same underlying concept) it was made so that the tag is automatically introduced as a regular identifier.

However, there is one behavior in C that had to be accounted for. You could have something like this:

struct prettyThing
{
  // data
};

void prettyThing();

And there's no ambiguity, due to the different namespaces. So C++ preserves this behavior. One could also have:

class prettyThing
{
   // members
};

void prettyThing();

Without ambiguity, the class name is hidden by the other identifier. It is only accessible with an elaborated-type-specifier, i.e. class prettyThing. That's one practical reason for why one may write the declaration as you see. And it's not academic, consider the POSIX stat and struct stat pair.

But... unless you are interfacing with C code that was originally written with the C notion in mind, there is no reason to write new C++ code that way.

One may wonder why it wasn't kept the same for types declared with struct and simply disallowed for types declared with class. To understand that, it's best to reiterate and remember that classes and structures are really meant to be the same thing in C++ (save for default access specifier). The following is all valid C++:

struct thing1; // forward declare

class thing1 {
  // define
};

struct thing2 {
   // define
};

class thing2 object;

Either keyword can be used in the elaborated-type-specifier, they are completely interchangeable in standard C++1.


1 - Microsoft ABI behaves as a bit of an oddball here, though.

like image 107
StoryTeller - Unslander Monica Avatar answered Nov 16 '22 02:11

StoryTeller - Unslander Monica