answersLogoWhite

0


Best Answer

Classes are not created, they are designed. A class is merely the definition of a type. The objects you instantiate from those classes are what are actually created, via the class constructors.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

A static member variable cannot be initialised inside a class declaration, it must be defined outside the class. It makes no difference if the member is constant or variable. Typically, statics are initialised in the class cpp file while the class is declared in the corresponding header.

// file: foo.h

class foo

{

static const int x;

};

// file: foo.cpp

#include "foo.h"

// initialise static member:

const int foo::x = 42;

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

You declare a static member constant the same as you would declare any static member variable, you just need to include the const keyword:

// file: foo.hpp class foo {

private: // or public, or protected, whichever access is required...

static const int my_const;

};

However, you must also initialise the constant, and this must be done from outwith the class declaration and outwith all other class definitions. Typically you will do this from the class .cpp file (containing the class definitions) rather than from the class .hpp file (containing the class declarations).

// file: foo.cpp

const int foo::my_const = 100;

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

#include<iostream.h> // include header file

using namespace std;

class person //class

{

char name[30];

Int age;

public:

void getdata(void);

void display(void);

};

void person :: getdata(void)

{

cout << "Enter name: ";

cin >> name;

cout << "Enter age: ";

cin >> age;

}

Void person : : display(void)

{

cout << "\nNameame: " << name;

cout << "\nAge: " << age;

}

Int main()

{

person p;

p.getdata();

p.display();

Return 0;

} //end of example

The output of program is:

Enter Name: VIPIN

Enter age:21

Name:VIPIN

Age: 21

The program define person as a new data of type class. The class person includes two basic data type items and two function to operate on that data. These functions are called member function. The main program uses person to declare variables of its type. As pointed out earlier, class variables are known as objects. Here, p is an object of type person. Class object are used to invoke the function defined in that class.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

To declare a class, a basic layout could look like this:

//any header files that are going to be required in the class should

//be defined here, for example

//example inclusion of header file

#include "name_of_header.hpp"

class NameOfClass

{

public:

//constructor

NameOfClass();

//destructor

~NameOfClass();

private:

//you can include any data members only accessible by the

//class here

};

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

A static member declaration is done in the same way as any other member variable.

However, I assume that you mean implementation, and not declaration.

If you're writing a class in a .hpp file, and implementing the class in a .cpp file, you need to create the static variable in the .hpp, and almost re-declare the variable in the .cpp file. For example:

.HPP FILE:

static int number;

.CPP FILE:

int nameofclass::number = 0;

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

Classes can be declared with the struct or classkeywords. Both do the same thing, the only difference is that struct members are public by default while class members are private by default. As such, the following definitions are equivalent:

class C1 {

int x;

};

struct C2 {

private:

int x;

};

By convention, we use a struct data type to represent plain old data types (PODs) just as we would in C. We use a class data type to represent types that hold an invariant such as a resource handle or smart pointer, or where the class representation needs to encapsulate data privately.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a class in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a c plus plus programme to illustrate single inheritance?

struct A {}; // base class struct B : A {} // derived class (single inheritance).


Can a c plus plus class be derived from a Java class?

No.


How do you write classes and objects for hospital management software using objective c or c plus plus?

You declare a class as follows: class MyClass { //some stuff here... } You create an object as follows: MyClass object; This is how you create classes and objects in C++.


Write abstract class in c plus plus?

An abstract class is any class definition that contains at least one pure-virtual function. class AbstractClass { public: virtual void DoSomething()=0; // Pure-virtual. };


What is the role of object in c plus plus?

An object in C++ is an instance of a C++ class.


What is nested class in c plus plus?

s.


What is the unit of programming in c plus plus A. Function B. class C. object D. Attribute?

B. Class.


Can you use c in c plus plus without using class and object?

Sure.


What is a method in c plus plus?

In C++, methods are simply class member functions.


How do you create a class in C plus plus?

class class_name { private: data_members; public: member_functions; };


What is object in c plus plus?

An object is simply an instance of a class.


What are the abstract datatypes in c plus plus?

Class Object Message