What does v=0 in function parameters means [duplicate] - c++

This question already has answers here:
assignment operator within function parameter C++
(3 answers)
Closed 5 years ago.
Here is a modified code from geeks for geeks.
#include<iostream>
using namespace std;
class Test {
int value;
public:
Test(int v = 0) {value = v;}
int getValue() {return value;}
};
int main() {
Test t(20);
cout<<t.getValue();
return 0;
}
What does parameter in function Test(int v=0) means?

That is default value for v parameter, which is the v variable will use that value if there is no value passed

Related

why default initialization using parentheses is not permissible in c++? [duplicate]

This question already has an answer here:
Why can in-class initializers only use = or {}? [duplicate]
(1 answer)
Closed 5 years ago.
This is the example from Effective modern c++.
class Widget {
…
private:
int x{ 0 }; // fine, x's default value is 0
int y = 0; // also fine
int z(0); // error!
};
direct initialization using ()
Inside the class treats the below
int z(0);
As a function as and expects parameter .As result error
Expected parameter declarator
alternatively can be done
class Widget {
private:
int x{ 0 };
int y = 0;
int z;
public:
Widget():z(0){}
};

C++: Whats the meaning of saying func()=30 [duplicate]

This question already has answers here:
Assigning value to function returning reference
(7 answers)
Closed 6 years ago.
What is the meaning of saying
fun()=30;
count<<fun();
Function definition is:
int &fun()
{
static int x = 10;
return x;
}
int& fun();
The return type of fun() is an integer reference.
fun() = 30
assign the value 30 to the integer referenced by the return value of fun().
Which integer?
int &fun()
{
static int x = 10; // <-- this one
return x;
}

Difference between "int f(int(fn)())" and "int f(int(*fn)())"? [duplicate]

This question already has answers here:
Function pointer vs Function reference
(4 answers)
Closed 6 years ago.
int g()
{
return 0;
}
int f1(int(fn)())
{
return fn();
}
int f2(int(*fn)())
{
return fn();
}
int main()
{
f1(g);
f2(g);
}
The code above can be sucessfully compiled.
I just wonder:
What's the difference between int f(int(fn)()) and int f(int(*fn)())?
Why does both of them are legal in C++?
int f(int(fn)()) and int f(int(*fn)()) are the same thing. A function takes a function pointer as parameter.
When we pass the name of a function as parameter, the function name is automatically converted to a pointer. So
int f(int(fn)()) {}
int f(int(*fn)()) {} // redefinition error

Need to understand when parameters passed as (className& arg) in c++ [duplicate]

This question already has answers here:
"const T &arg" vs. "T arg"
(14 answers)
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 7 years ago.
// const objects
#include <iostream>
using namespace std;
class MyClass {
int x;
public:
MyClass(int val) : x(val) {}
const int& get() const {return x;}
};
void print (const MyClass& arg) { // Need to understand this line
cout << arg.get() << '\n';
}
int main() {
MyClass foo (10);
print(foo);
return 0;
}
I am new to C++. Need to understand what are the parameters passed in print function. If this is address then why are we passing foo is print function call.
The & in void print (const MyClass& arg) that arg is passed by reference. It is C++s way to make pointers and things a little bit easier.
References allow you to manipulate a variable inside of a function and make the results visible on the outside too. So a bit like pointers. But you don't need to explicitly get the address of the variable to do that.
The const statement is a way to prevent the described behavior. const forbids the manipulation of arg inside print.

When we use sizeof operator on object of that class which do not have any data members. Then what would be the output? [duplicate]

This question already has answers here:
C++: What is the size of an object of an empty class?
(17 answers)
Closed 9 years ago.
When we use sizeof operator on object of that class which do not have any data members. Then what would be the output? Why this always output 1??
class Abhi
{
public :
int sum (int, int);
int avg (int);
};
int Abhi:: sum(int a, int b)
{
float c;
c=a+b;
return 0;
}
int main()
{
Abhi abh;
int c;
c= sizeof(abh);
cout<<c;
}
According to the standard, each instance of a class must have a unique address. This is usually implemented by giving an "empty" class (no data members) a minimum size, as if there is a dummy char inside. This is why sizeof gives 1 and not 0.