Delegating Constructors [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am learning about Delegating Constructors.
#include <iostream>
using namespace std;
class A{
public:
A(int i, int j): num1(i), num2(j){
average=(num1+num2)/2;
}
A(): A(0){ }
A(int i): A(i, 0){ }
private:
int num1;
int num2;
int average;
};
and this is what I succeed to understend. I don't know such it works in a int main().

I think you need just an example to how to create objects from A:
int main()
{
A obj1(10, 20); // Calls A(10, 20) average: 15
A obj2; // Calls A() -> A(0) -> A(0, 0) average: 0
A obj3(100); // Calls A(100) -> A(100, 0) average: 50
}

Related

How to move functions into a namespace? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 months ago.
Improve this question
The project calls functions from a library. I want to move those functions behind a namespace so that is easier to spot the places on the codebase where those functions are being called.
How functions are being called:
#include "foo.h"
int main()
{
foo();
bar();
return 0;
}
How I want to call them:
#include "myfoo.h"
int main()
{
thatlibrary::my_foo();
thatlibrary::my_bar();
return 0;
}
How I implemented that:
myfoo.h
namespace thatlibrary
{
void my_foo();
void my_bar();
}
myfoo.cpp
namespace thatlibrary
{
void my_foo()
{
foo();
}
void my_bar()
{
bar();
}
}
Wondering if there is any other solution? Perhaps more elegant.
Just use a using declaration in your namespace:
// global namespace
int foo() {
return 1;
}
namespace thatlibrary {
using ::foo;
}
auto i = thatlibrary::foo();
Notice that the name foo is still available in the global namespace, though.
// in global namespace
auto j = foo(); // works just fine
// from anywhere
auto k = ::foo(); // works just fine
I think the library is poorly designed, if it declares all its stuff in the global namespace.

Why is it giving the error : "abc is inaccessible"? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
#include <iostream>
using namespace std;
class B;
class A
{
int a = 10;
int b = 20;
public:
friend void A::display(B&a);
void display(B & c);
};
class B
{
int abc = 20;
public:
friend void A::display(B&a);
};
void A::display(B & c)
{
cout << c.abc;
}
int main()
{
B b;
A a;
a.display(b);
return 0;
}
it is giving the error on the 21th line that "abc" is inaccessible. Although it is a friend function and have access to B class' private data members

C++ constructor definition differences in the code given below [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am newbie to C++. Learning constructors. Please refer to two codes mentioned below, and provide reason, why Code 2 is not working. Thanks.
Code 1:
#include <iostream>
using namespace std;
class Box
{
int x;
public:
Box::Box(int a=0)
{
x = a;
}
void print();
};
void Box::print()
{
cout << "x=" << x << endl;
}
int main()
{
Box x(100);
x.print();
}
Code 2:
#include <iostream>
using namespace std;
class Box
{
int x;
public:
Box(int a=0);
void print();
};
Box::Box(int a=0)
{
x = a;
}
void Box::print()
{
cout << "x=" << x << endl;
}
int main()
{
Box x(100);
x.print();
}
Why the code 1 is working but Code 2 is NOT working?
For some odd reasons you are not allowed to repeat the default value for a parameter:
class Box
{
int x;
public:
Box(int a=0);
//------------^ given here
void print();
};
Box::Box(int a=0)
//------------^^ must not be repeated (even if same value)
{
x = a;
}

Create a variable inside another statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is there a way to allocate a variable within the scope of a parameter list? By using new we can do the following :
Class A{ /*... snip ...*/ };
void myFunc(A* a){ }
int main(...){
myFunc(new A());
return 0;
}
This will create a new A. What if the signature of myFunc was
void myFunc(A a);
instead. Is there a syntax to create local instance inside the myFunc() parameter list? I'm looking for something like
myFunc(A());
or
myFunc(A a());
Another use would be for something like :
A a(123);
if(a == A(123)){ }
The net effect is to save one line, but it also creates a scope within the parameters list which makes me wonder if it is allowed at all.
If you just want to create a variable to pass to the function you can use a aggregate initialization / list initialization
#include <iostream>
#include <cmath>
using namespace std;
class A{ /*... snip ...*/ };
void myFunc(A a){ }
int main(){
myFunc(A{});
return 0;
}
Live Example
You can also use this with classes that have constructors that take multiple parameters
#include <iostream>
#include <cmath>
using namespace std;
class A
{
private:
int foo;
int bar;
double foobar;
public:
A(int a, int b, double c) : foo(a), bar(b), foobar(c) {}
};
void myFunc(A a){ }
int main(){
myFunc(A{1,2,3.0});
return 0;
}
Live Example
C++ supports this with the myFunc(A()); syntax you posed in your question.
#include <stdio.h>
char lazybuff[500];
class Point
{
public:
Point (double x, double y) : m_x(x), m_y(y) { }
char * ToString (void) { sprintf (lazybuff, "%f, %f", m_x, m_y); return lazybuff; }
private:
double m_x, m_y;
};
void print_point (Point print_me)
{
printf ("%s\n", print_me.ToString());
}
int main (void)
{
print_point (Point(5, 3));
return 0;
}

declare function's type as struct c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So I have this class named User. Inside I have made a struct called flights and I want to make a function in this class to return a struct with the values of flights. Is that possible? Something like the follow. I know it doesn't work but is there a way?
Class User
{
string name, surname...;
struct flights
{
int miles;
double cost;
}
struct add_miles(reads from another class);
}
struct User::add_miles()
{
return flights;
}
I am not sure I understand your requirement clearly but see my try:
#include<iostream>
class User
{
//string name, surname...;
public:
struct flights
{
int miles;
double cost;
}myflights;
struct flights add_miles()
{
return myflights;
}
};
int main()
{
User me;
me.myflights.miles=100;
std::cout<<me.add_miles().miles;
}
You're going to have to make add_miles return a flights struct rather than just a struct.
Example code:
#include <iostream>
using namespace std;
class User
{
private:
string name,
surname;
public:
struct flights {
int miles;
double cost;
};
flights add_miles() {
return flights();
}
};
int main(int argc, char **argv) {
User u;
User::flights f = u.add_miles();
cout << f.miles << endl;
return 0;
}