answersLogoWhite

0

What function initializes variables in a class?

Updated: 8/11/2023
User Avatar

Deepsvit

Lvl 1
14y ago

Best Answer

you have to initialize it into the "Constructor" which is a function without dataType and it's name is the same with the class name

EX:-

class Exforsys

{

private:

int a,b;

public:

Exforsys();

...

};

Exforsys :: Exforsys()

{

a=0;

b=0;

}

the EX from : http://www.exforsys.com/tutorials/c-plus-plus/class-constructors-and-destructors-in-c.html

I recommend this link for You Under the title "Constructors and destructors" :

http://www.cplusplus.com/doc/tutorial/classes/

You can also See : http://www.win.tue.nl/~maubach/university/education/online-references/cpp/swartz/notescpp/oop-condestructors/constructors.html

User Avatar

Wiki User

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

Wiki User

12y ago

The constructor.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

constructor

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What function initializes variables in a class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What actually happens when function get called how the compiler is reading the next instruction after completing that function?

When a function gets called, the processor first stores the address of the calling function in a structure called activisation structure and jumps to the called function. Then it allocates the memory for the local variables and initializes them. Then it does the processing in that function. After that it deallocates the memory allocated for the local variables and returns to the calling function. When a function gets called, the processor first stores the address of the calling function in a structure called activisation structure and jumps to the called function. Then it allocates the memory for the local variables and initializes them. Then it does the processing in that function. After that it deallocates the memory allocated for the local variables and returns to the calling function.


When an array is declared does c automatically initializes its elements to zero?

For global/static variables: yes.For auto variables: no.


Why compilor initializes 0 to global variables?

By design. What else should it do? Of course you can initialize your variables explicitly: double pi = 3.0;


Is it True or False that Variables declared in the particular member function are know as data members and can be used in all member functions of the class?

False. Variables declared within a particular member function are local to that function, and are automatically allocated and deallocated at entry and exit of the function.


Why static functions cannot use instant variables?

Static functions are tied to a class, not to a particular object. A static function can only access static variables because it has no knowledge of member variables.


What is the difination of linear function?

A linear function is any function that graphs to a straight line. What this means mathematically is that the function has either one or two variables with no exponents or powers. If the function has more variables, the variables must be constants or known variables for the function to remain a linear function.


Is it possible to access the private variable in CPP from outside the class?

Private variables can only be accessed from outside of a class by using any public function of that same class. Or this can be accomplished by using Friend functions.


What are independent variables and dependent variables in math?

Independent variables are the input value of a function (usually x) and dependent variables are the output value of the function (usually y).


What are instance variables?

These are normal variables declared within a class that are attached to an object instance of a class.


What function will be called by default to initialize the member variables of the object of the class?

The constructor. It's run each time a new object is created, usually setup to initialize member variables, but it can do most anything.


What is class variable?

Class Variables or Instances variables are variables that are declared inside a class and are available for the whole class. They are available for all instances of that class and are not specific to any method. Ex: public class Test { private String name = "Rocky"; } Here name is a class variable.


What is meant by a class in java programming?

Class - A class can be defined as a template/ blue print that describe the behaviors/states that object of its type support.Classes in Java:A class is a blue print from which individual objects are created.A sample of a class is given below: public class Dog{ String breed; int age; String color; void barking(){ } void hungry(){ } void sleeping(){ } }A class can contain any of the following variable types.Local variables . variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.Instance variables . Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.Class variables . Class variables are variables declared with in a class, outside any method, with the static keyword.A class can have any number of methods to access the value of various kind of methods. In the above example, barking(), hungry() and sleeping() are variables.