Question

Hello I need help with this questions about C++ programming - If a function is called...

Hello I need help with this questions about C++ programming

- If a function is called and passed a pointer variable as a parameter, how must that pointer variable be passed if the function is to modify its contents?

- When must a class provide a getter for a member variable?

- An inline function saves processing time, but at what expense?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. If a function is called and passed a pointer variable as a parameter, how must that pointer variable be passed if the function is to modify its contents?

Function is called and passed a pointer variable as a parameter. In order for the passed pointer variable to be modifiable we need to pass the pointer with a non-const modifier so that the pointer can be modified within the function.Also the variable passed must also be non-const type, as modifier const doesn't allow the variables to be modified. If we call the function using a non-pointer variable , we need to pass the variable using the reference so that the address of the variable is passed to the function rather than the value.Similarly, if we pass a pointer type to the function, we need to pass the pointer name without any asterisk(*) as pointer name refers to the memory address of the variable and * refers to the variable stored in the variable.

Eg: Suppose we have the below function which is passed a pointer variable such that the pointer variable can be modified within the function:

// the pointer must not contain const keyword as we need to modify the pointer within the function
void func1(int *p)
{
   *p = *p+5; // we add 5 to the existing contents of pointer value
}

// Calling the function
int main()
{
   int a = 10;
   int *p = &a; // p is pointing to a i.e memory address of a. It must be of non-const type
   // calling the function using the pointer variable
   // p refers to the address of a
   func1(p) ;
   // calling the function using the regular variable
   // address of a is passed to the function
   funct1(&a);
   return 0;
}

2. When must a class provide a getter for a member variable?

A class must provide a getter for a member variable when it is declared in non-public(private or protected) visibility mode. As only public variables are directly accessible by the objects of the class. The private members of the class are only accessible by the member functions of the class and protected members are accessible by the class member functions or its derived class's member functions.
Eg-

class Sample
{
private :
   int a;
protected:
   int b;

public:
   int c;
   Sample() : a(2), b(3), c(5)
   {}
  
   int getA() { return a;}
   int getB() { return b; }  
      
};

In the above class Sample, getters for member variables a and b are required as the objects of class Sample cannot directly access them but c being in public visibility mode is directly accessible by the objects of class Sample and hence getter for member variable c is not necessary

3. An inline function saves processing time, but at what expense?

An inline function is a function for which the function call is replaced by the function definition in compile time i.e the compiler replaces all the function call in the program by its function definition. This saves processing time as it reduces the function call overhead time because at the time of function call it doesn't have to refer to the memory address where the function is defined.
The variables used in function is also increased and hence the registers used for the calculation is increased. This leads to increase in variable numbers. For small functions , making it inline doesn't affect the size of the program much and hence can be ignored. But if a large function is made inline then the size of the program and hence the executable will be large. Also if a number of smaller functions are made inline then the total effect of these functions on the executable will be large. Also inline functions may reduce the run time of the program but the compile time of the program.

Add a comment
Know the answer?
Add Answer to:
Hello I need help with this questions about C++ programming - If a function is called...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello, I have my piece of C programming code and I have a pointer initialized to...

    Hello, I have my piece of C programming code and I have a pointer initialized to point to my array and I need help to display the contents of my array using a while loop or do-while loop. The stop condition should be when the pointer reaches '\0'. The code is below: #include <stdio.h> int main () {    char array[80]; printf("Enter a string: "); scanf("%[^\n]", &array); printf("%s\n", array); char * p;    p = array;         }

  • Hello! I know this has been answered before but I would love a new solution for it. This would be in C++ Create a class...

    Hello! I know this has been answered before but I would love a new solution for it. This would be in C++ Create a class named Building with one public pure virtual function computerEnergyConsumption() that returns a double value of the instance variable which stores the energy consumed by the building. Create the two classes SolarBuilding and WindMill that both publicly inherit from Building. SolarBuilding has an instance variable that stores the energy generated by the solar panels. WindMill has...

  • Hello, I need questions for a survey about instagram (as a business) the focused questions must...

    Hello, I need questions for a survey about instagram (as a business) the focused questions must be about classification differentiation extensions quality evaluations channel distribution we need to come up with a big idea for marketing instagram please help thank you

  • Hello. I just need help with my c++ code. Write a program that: Creates an integer...

    Hello. I just need help with my c++ code. Write a program that: Creates an integer variable AND an integer pointer variable Ask the user to input an integer value Store the value in the integer variable Print the address of the variable. Make the pointer variable point at the integer value Print the value pointed to by the pointer Print the address of the pointer

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Hello, I need a c program called int* create_random_array(int n) that returns a random array of...

    Hello, I need a c program called int* create_random_array(int n) that returns a random array of size an. Alternatively, you can initialize a pointer to an array (called Rand) and write a function called void(create_array(int * Rand, int n) which assigns an array of size n to Rand. Note: Plese utilize rand(fi) and srand() from stdlib.h for this code

  • Hey everyone, I need help making a function with this directions with C++ Language. Can you...

    Hey everyone, I need help making a function with this directions with C++ Language. Can you guys use code like printf and fscanf without iostream or fstream because i havent study that yet. Thanks. Directions: Write a function declaration and definition for the char* function allocCat. This function should take in as a parameter a const Words pointer (Words is a defined struct) The function should allocate exactly enough memory for the concatenation of all of the strings in the...

  • Hello, In need of help with this Java pa homework. Thank you! 2. Create a normal...

    Hello, In need of help with this Java pa homework. Thank you! 2. Create a normal (POJo, JavaBean) class to represent, i. e. model, varieties of green beans (also known as string beans or snap beans). Following the steps shown in "Assignment: Tutorial on Creating Classes in IntelliJ", create the class in a file of its own in the same package as class Main. Name the class an appropriate name. The class must have (a) private field variables of appropriate...

  • c++ I need help! create Student.h In this class, you are provided with a class skeleton...

    c++ I need help! create Student.h In this class, you are provided with a class skeleton for the Student type. This type should contain two member variables: a string called name a vector of doubles called grades Additionally, you should declare the following functions: A constructor which accepts a single string parameter called name A void function, addGrade which accepts a single double parameter and adds it to the grades vector A function which accepts no parameters and returns the...

  • Hi could you guys help me with this 3 questions tho and yes I'll rate you...

    Hi could you guys help me with this 3 questions tho and yes I'll rate you up thank you! 5. A C++ class is most similar to a(n) a. inline function b. pointer c. library function d. structure e. reference 6. A __________ is a member function that is automatically called when a class object is __________. a. constructor, created b. destructor, created c. static function, deallocated d. utility function, declared e. None of these 7. Which is the base...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT