C++ define 3 arguments but using just 2 [duplicate] - c++

This question already has answers here:
Default arguments in constructor--C++
(4 answers)
Closed 8 years ago.
I would like to use different number of arguments.
class A {
public:
A(int a, int b);
};
A::A(int a, int b) {
// constructor code
}
int main() {
A a(5); // I use only 1 argument and the second one I let default ?
}

Constructors are (a bit special) functions - regular default parameter syntax applies.
class A {
public:
A(int a, int b = default_value);
};
A::A(int a, int b) {
// constructor code
}
int main() {
A a(5);
}

Related

Removing code repetition using casting operators [duplicate]

This question already has answers here:
How do I remove code duplication between similar const and non-const member functions?
(21 answers)
Elegant solution to duplicate, const and non-const, getters? [duplicate]
(8 answers)
Avoid literally duplicating code for const and non-const with auto keyword?
(3 answers)
Closed 6 days ago.
class nums
{  public:
int sum(int a,int b) const
{  
return a+b;
}
int sum(int a,int b)
{  
return a+b;
}
};
int main()
{
const nums a;
a.sum(5,10);
nums  b;
b.sum(1,2);
}
I am trying remove two functions for const objects and non const objects replace with one function using casting operators.
You can use int sum(int a,int b) const even if the object isn't marked as const. See this answer.
So, the following code would compile:
class nums
{
public:
int sum(int a,int b) const
{
return a+b;
}
};
int main()
{
const nums a;
a.sum(5,10);
nums b;
b.sum(1,2);
}
Link to Compiler Explorer.

How does my memory behave in this example? [duplicate]

This question already has answers here:
Question about constructors & memory leak
(2 answers)
Rule of Three in C++
(3 answers)
Closed 4 months ago.
So I m creating two classes A and B and I want to practice type casting. So in this this example
I m creating 2 variables , 'foo'(class A type) and 'bar'(class B type) in the stack and when casting from B TO A using 'foo = bar;' I m creating new variable in the Heap and p pointer to it I want my variable 'foo' to point to that variable.I m doing that right ?? and how do I check if everything is working fine ( the value of foo has changed to 7 ) you can view the code and tell me more about what's going on and is it possible to let foo point to the pointer or not??
class A {
public:
int a;
A(int x)
{
a = x;
}
};
class B {
public:
int x;
// conversion from A (constructor):
B(int a)
{
x=a;
}
// B (const A& x) {cout<<"conversion done"<<endl;}
// conversion from A (assignment):
B& operator= (const A& c) {this->x =c.a ;return *this;}
// conversion to A (type-cast operator)
operator A() {
cout<<"typecast"<<endl;
A *p = (new A(x));
cout<<p<<endl;
return *p;
}
};
int main ()
{
A foo(5);
cout<<&foo<<endl;
B bar(7);
foo = bar;
cout<<&foo<<endl;
return 0;
} ```
the output:
0x5b73fff75c
typecast
0x201cdfc1580
0x5b73fff75c

Initialize parameter into constructor, other than the first one

I want to explicitly change the second parameter in a constructor of a struct, in the following scenario. Is it possible, if so, how?
struct foo{
int x;
int y;
foo(int a=4, int b=6){
x=a;
y=b;
}
};
int main(){
foo *f = new foo();
cout<<f->x<<" "<<f->y<<endl;
//4 6
foo *g = new foo(3,4);
cout<<g->x<<" "<<g->y<<endl;
//3 4
foo *h = new foo(3);
cout<<h->x<<" "<<h->y<<endl;
//3 6
//Can something like this be
//done in C++, if I want
//to change the value of the
//second variable only
foo *k = new foo(b = 13);
return 0;
}
Is it possible, if so, how?
It is not possible with constructor. In general, c++ does not support named keyword arguments to functions, and it is not possible to skip arguments even if they have a default, if you want to pass a non-default after it.
It will be possible without constructor using list initialisation syntax since C++20 using designated initialisers, if you use default member initialisers:
struct foo{
int x = 4;
int y = 6;
};
int main(){
foo f {.y = 4};
}
You can achieve something similar with tag dispatching; No need for future standard:
struct foo{
int x = 4;
int y = 6;
enum Xtag { Xinit };
enum Ytag { Yinit };
foo(int a, int b) : x(a), y(b) {}
foo(Xtag, int a) : x(a) {}
foo(Ytag, int b) : y(b) {}
};
int main(){
foo f(foo::Yinit, 4);
}
A solution using lambda that can be used without modifying an existing class. Following works with your definition of foo:
auto make_foo_x4 = [](int b) {
return foo(4, b);
};
foo f = make_foo_y(4);
The downside is that we have to explicitly repeat the default value of x, so this can break assumptions if the default is changed in class definition.

whenever i try to run this code it gives me an error

#include<iostream>
#include<conio.h>
using namespace std;
class one
{
int r;
public:
one(int a) : r(a) {}
void set(int a) { r = a; }
int area() { return r*r*3.14; }
};
class two
{
one x;
int hi;
public:
two(int r, int h)
{
hi=h;
x.set(r);
}
int v() { return x.area()*hi; }
};
int main()
{
_getch();
return 0;
}
the error is:no appropriate default constructer available.
would you mind helping me so that i can get rid of this error.
//.............................................................................
When you declare x in two, C++ basically tries to create an instance of one using a constructor without any parameters (default constructor), since "null objects" do not exist in C++.
one does not provide a default constructor, but it provides the constructor one(int a), so instead of one x;, use one x(0); or one x = one(0);

C object with () - what does it do? [duplicate]

This question already has answers here:
What does A a() mean? [duplicate]
(2 answers)
Closed 9 years ago.
What does this particular piece of code do? To be more precise, what does test tob(); do?
class test {
private:
int a;
int b;
public:
test (int);
test();
};
test::test() {
cout<<"default";
}
test::test (int x=0) {
cout<<"default x=0";
}
int main() {
test tob();
}
I dont know what does test tob(); do, but it isn't giving any compilation errors.
test tob();
This declares a function which return type is test. It does not create an object. It's also know as most vexing parse.
To create a test object:
test tob;
Also, the way you define a function(include constuctor) with default argument is incorrect.
test::test (int x=0) { // incorrect. You should put it in function when it's first declared
cout<<"default x=0";
}
Below code should work:
class test {
int a;
int b;
public:
explicit test (int = 0); // default value goes here
};
test::test (int x) {
cout<<"default x=0";
}
int main() {
test tob; // define tob object
}