answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How you declare class scoped variables and member functions?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What are the application of this pointer can you use this pointer in friend function justify?

The this pointer can only be used within nonstatic member functions. Friend functions are not members so they have no access to a this pointer. However, you can pass a specific instance of a class to a function via a class reference argument. To understand how friendship works, first understand that a nonstatic member function has the following properties: 1. It has private access to the class. 2. It is scoped to the class. 3. It must be invoked upon an object of the class (has a this pointer). Static member functions have the first two properties while friend functions only have the first property.


Explain static data member with the help of example?

Yes. Static data members are local to the class in which they are declared. Thus all instances of the class share the same variables (unlike non-static data members where each instance of the class has its own set of variables). Moreover, since static data members do not belong to any instance of the class, they are accessible without the need to instantiate an instance of the class, and like all other static variables, remain in scope for the entire duration the program is running. Also, as with all other static variables, they must be initialised at compile time from outside of the class declaration. Usually this is done from the class CPP file.Static member functions are similar to static data members in that they are local to the class, rather than to an instance of the class. Since they do not belong to any instance of the class, they do not inherit an implicit this pointer. As a result, they are accessible without the need to instantiate an instance of the class and will remain in scope for the entire duration the program is running.It is not unusual for a class to have both static data members and static member functions. They can be likened to global variables and global methods, but scoped to the class. However, their visibility can be restricted by the access specifiers enforced upon them (public, protected or private). Although static member functions cannot access instance methods and instance variables (unless an instance is physically passed to them as an argument) they have unrestricted access to the static data members of the class, as do all instances of the class and friends of the class.A classic example of static member functions and static data members in the same class is when one needs to maintain a count of all instances of a class. All the class constructors must increment the static counter while the destructor must decrement it. A static member function such as GetCount() can then report the number of instances currently instantiated, even when there are no instances.There are many other uses, but the golden rule is that they must be related to the class in which they are declared. If their purpose is simply to provide global functionality then declare them as such, or (better) limit their scope by declaring them in a separate class specifically for that purpose with private constructors to prevent any instances from being created (since none would be required).


How does friend operator function differ from member operator function?

In C++, we know that private members cannot be accessed from the outside class.That is a non-member function cannot have an access to the private data of a class.However there could be a situation where we would like two classes to share a particular function.For example consider a case where two classes, manager and scientist have been defined.We would like to use a function income_ tax () to operate on the objects of both these classes.In such situation, C++ allows the common function to be made friendly with both the classes. Such a function needs not be member of any these classes.To make outside function friendly to a class, we have to simply declare this function as a friend of a class as shown below:Class ABC{………..Public:……..……..Friend void xyz (void); //declaration};When the function is logically coupled with the class (like your maze connectedness example)When the function needs to access private or protected members, it's better to make it a member than a friend. when it's a generic function that can be templatized to naturally work on other classes (look at the header for good example) .


When is a friend function compulsory?

A normal member function has the following qualities: 1. Has private access to the class. 2. Is scoped to the class. 3. Can be invoked on an instance of the class. Static functions have the first two qualities only. Friend functions have the first quality only. It therefore follows that friend functions are only compulsory when you need quality 1 only.


Static variable in c plus plus?

There are two uses for a static variable in C++. When declared outside of a class, a variable is regarded as being global. However a static variable is deemed local to the file in which it is declared. That is, the variable is scoped to the file, and cannot be accessed by code outside of that file. This aspect was inherited from C. C++ also allows static variables to be declared inside a class. In this case, the variable is local to the class. By contrast, instance variables (non-static member variables) are local to each instance of the class. With static variables, there is only one instance of each variable which can be shared by all instances of the class. It is not unlike a global but it is scoped to the class. Since all static variables are instantiated at compile time, they exist for the entire duration a program runs. Even if they fall from scope, they never lose their value. Static variables defined within a class are also available even when no instances of the class are instantiated. Their visibility outside of the class is dependent upon whether they are declared public, protected or private.

Related questions

What do you mean by classes within the classes?

You can declare and implement a class within a class...class abc {...class def {...}}The class def is scoped and known only within the context of abc. It is possible to declare instances of def as member variables of abc, however, for this to work correctly, they should be private, because methods of def, not even public methods, do not exist outside of the scope of abc.


How is working of a member function different from a friend function and a non-member function?

With respect to a given class, all functions can be split into four categories: 1. Member functions. 2. Static member functions. 3. Friend functions. 4. Non-member functions. All class member functions have the following three properties with respect to the class in which they are declared a member: 1. Private access to the class representation. 2. Scoped to the class. 3. Invoked through an instance of the class (has a 'this' pointer). Static member functions have the first two properties only. Friend functions have the first property only. Non-member functions have none of these properties.


Is a public function accessed like a non-member function?

A public function is scoped to the class in which it is declared. If declared non-static, then it must be invoked against an instance of the class but if declared static then namespace resolution is required to access the function. A non-member function is not scoped to any class but may be scoped to a namespace. If no namespace is specified, then it is scoped to the (unnamed) global namespace. If scoped to any other namespace then namespace resolution is required to access the function.


What are the application of this pointer can you use this pointer in friend function justify?

The this pointer can only be used within nonstatic member functions. Friend functions are not members so they have no access to a this pointer. However, you can pass a specific instance of a class to a function via a class reference argument. To understand how friendship works, first understand that a nonstatic member function has the following properties: 1. It has private access to the class. 2. It is scoped to the class. 3. It must be invoked upon an object of the class (has a this pointer). Static member functions have the first two properties while friend functions only have the first property.


Why friend are used in object oriented programing?

A friend function is a function that cannot be declared a member of a class but which requires private access to that class. For example, a function that operates upon two different classes cannot be a member of both classes, but if the function requires private access to both classes then it has to be a friend to at least one of them.To fully appreciate friend functions, consider that a non-static member function has the following three properties:Has private access to the class.Is scoped to the class.Must be invoked against an object of the class (has a 'this' pointer).Static member functions have the first two properties only while friend functions have the first property only. All other non-member functions have none of these properties.


Is a static member function similar to a friend function?

No. To understand how friend functions relate to static functions you first need to understand the three qualities of a normal member function (an instance member function): 1. The function has access to the private aspects of the class in which it is declared. 2. The function is scoped to that class. 3. The function must be invoked on or from within an object of that class. A static function only has the first two qualities (so no instance of the class is required) while a friend function only has the first quality (so is neither scoped to the class nor requires an instance of the class). All three share the first quality only but that alone does not make them similar. It is the qualities they lack that sets them apart.


When was martin Luther king jr quickscoped?

During the summer of 2012 he was quick scoped by a member of FaZE to this day we still don't know what member of FaZE quick scoped him, but we assume that it was FaZE Banks, as he is the leader of the illuminati and also FaZE clan. We can not be sure about this assumption but we are mighty sure it was FaZE with the illuminati plotting something horrible.


What are the release dates for Scoped - 2011?

Scoped - 2011 was released on: USA: 21 May 2011


Explain static data member with the help of example?

Yes. Static data members are local to the class in which they are declared. Thus all instances of the class share the same variables (unlike non-static data members where each instance of the class has its own set of variables). Moreover, since static data members do not belong to any instance of the class, they are accessible without the need to instantiate an instance of the class, and like all other static variables, remain in scope for the entire duration the program is running. Also, as with all other static variables, they must be initialised at compile time from outside of the class declaration. Usually this is done from the class CPP file.Static member functions are similar to static data members in that they are local to the class, rather than to an instance of the class. Since they do not belong to any instance of the class, they do not inherit an implicit this pointer. As a result, they are accessible without the need to instantiate an instance of the class and will remain in scope for the entire duration the program is running.It is not unusual for a class to have both static data members and static member functions. They can be likened to global variables and global methods, but scoped to the class. However, their visibility can be restricted by the access specifiers enforced upon them (public, protected or private). Although static member functions cannot access instance methods and instance variables (unless an instance is physically passed to them as an argument) they have unrestricted access to the static data members of the class, as do all instances of the class and friends of the class.A classic example of static member functions and static data members in the same class is when one needs to maintain a count of all instances of a class. All the class constructors must increment the static counter while the destructor must decrement it. A static member function such as GetCount() can then report the number of instances currently instantiated, even when there are no instances.There are many other uses, but the golden rule is that they must be related to the class in which they are declared. If their purpose is simply to provide global functionality then declare them as such, or (better) limit their scope by declaring them in a separate class specifically for that purpose with private constructors to prevent any instances from being created (since none would be required).


How does friend operator function differ from member operator function?

In C++, we know that private members cannot be accessed from the outside class.That is a non-member function cannot have an access to the private data of a class.However there could be a situation where we would like two classes to share a particular function.For example consider a case where two classes, manager and scientist have been defined.We would like to use a function income_ tax () to operate on the objects of both these classes.In such situation, C++ allows the common function to be made friendly with both the classes. Such a function needs not be member of any these classes.To make outside function friendly to a class, we have to simply declare this function as a friend of a class as shown below:Class ABC{………..Public:……..……..Friend void xyz (void); //declaration};When the function is logically coupled with the class (like your maze connectedness example)When the function needs to access private or protected members, it's better to make it a member than a friend. when it's a generic function that can be templatized to naturally work on other classes (look at the header for good example) .


What is friend function and its advantages?

The best way to understand the advantage of friend functions is to consider that a member function has the following properties:1. Private access to the class representation.2. Scoped to the class.3. Can be invoked upon an instance of the class (has a this pointer).A static member function only has the first two properties while a friend function only has the first property. All other function have none of these properties. This gives the programmer fine-grained control over which functions can access which parts of the class representation.


Static variable in c plus plus?

There are two uses for a static variable in C++. When declared outside of a class, a variable is regarded as being global. However a static variable is deemed local to the file in which it is declared. That is, the variable is scoped to the file, and cannot be accessed by code outside of that file. This aspect was inherited from C. C++ also allows static variables to be declared inside a class. In this case, the variable is local to the class. By contrast, instance variables (non-static member variables) are local to each instance of the class. With static variables, there is only one instance of each variable which can be shared by all instances of the class. It is not unlike a global but it is scoped to the class. Since all static variables are instantiated at compile time, they exist for the entire duration a program runs. Even if they fall from scope, they never lose their value. Static variables defined within a class are also available even when no instances of the class are instantiated. Their visibility outside of the class is dependent upon whether they are declared public, protected or private.