Using nested class in head class constructor - c++

I want to make something that I pasted in code.
I want to use Nested class in Head class, look on code below.
What should I do? I was trying to use a nested construktor in initialization list but still not work. Any ideas?
class Head{
private:
int x;
public:
Head(int x, const Nested& n){
this->x=x;
}
class Nested{
private:
int a;
int b;
public:
Nested(int a, int b){
this->a=a;
this->b=b;
}
}
}

You mean you have a compile error? You should define Nested before its use, as below:
class Head{
private:
int x;
public:
class Nested {
private:
int a;
int b;
public:
Nested(int a, int b){
this->a=a;
this->b=b;
}
};
Head(int x, const Nested& n){
this->x=x;
}
};
int main()
{
Head::Nested n(0, 0);
Head h(0, n);
}

Related

friend function in C++ is not accessing private members

I am learning C++ and I have written the below given simple program to understand the working of friend function
(ignore all the complication I made by using complex syntax in the code because I am learning and I practice the syntax that I learn in programs).
The friend function is not accessing the private members of test and stu.
#include<iostream>
#include<conio.h>
class test;
class stu
{
private:
int z;
public:
stu(int z)
{
this->z=z;
}
friend disp(stu,test);
~stu(void)
{
std::cout<<"Destructor of stu class is executed!!"<<std::endl;
}
};
class test{
private:
int x;
public:
test(int a)
{
x=a;
}
friend disp(stu,test);
~test(void)
{
std::cout<<"Destructor is executed!!"<<std::endl;
}
};
class test2:public test
{
private:
int b;
public:
test2(int b)
{
this->b=b;
}
void show(void);
~test2(void)
{
std::cout<<"Destructor of second class executed!!"<<std::endl;
}
};
int main()
{
test t1(3);
test2 t2(5);
t2.show();
stu s1(10);
disp(s1,t1);
return 0;
}
void test2::show(void)
{
std::cout<<"Value of k = "<<b<<std::endl;
}
void disp(stu s2, test t2)
{
int sum;
sum = s2.z + t2.x;
std::cout<<"Sum = "<<sum<<std::endl;
}
Try to define the disp function before the main function :
void disp(stu s2, test t2)
{
int sum;
sum = s2.z + t2.x;
std::cout<<"Sum = "<<sum<<std::endl;
}
int main()
{
test t1(3);
test2 t2(5);
t2.show();
stu s1(10);
disp(s1,t1);
return 0;
}
and change the disp function signature as:
friend void disp(stu,test);

Can you construct after instantiation? Avoiding empty constructor

I have a class which has a member attribute consisting of an object defined elsewhere. In the code below, A contains a public attribute var which is a B:
class B {
public:
int x, y;
std::vector<int> z;
B(int a, int b, std::vector<int> c) {
x = a; y = b; z = c;
}
};
class A {
public:
B var;
A(int i, int j) {
std::vector<int> someVector;
B(i, j, someVector);
}
};
int main() {
A foo(5, 3);
return 0;
}
This (obviously) doesn't compile as var is instantiated upon an instantiation of A, too late for it to be constructed.
The best way I can do something similar is modify some code:
class B {
public:
int x, y;
std::vector<int> z;
B() {}
void setAttributes(int a, int b, std::vector<int> c) {
x = a; y = b; z = c;
}
};
class A {
public:
B var;
A(int i, int j) {
std::vector<int> someVector;
B.setAttributes(i, j, someVector);
}
};
This does compile because attributes are set after instantiation.
But is there a way to remain closer to the first code snippet?
A(int i, int j) : var(i, j, {}) {}
Also, in your code B(i, j, someVector); does not initialize member variable var, and B.setAttributes(i, j, someVector); wouldn't compile at all.
if you cannot define a useful default constructor and don't want the ugly two step initialization, I guess there is no way around a pointer to B. Something like
#include <memory>
class B {
public:
int x, y;
std::vector<int> z;
B(int a, int b, std::vector<int> c) {
x = a; y = b; z = c;
}
};
class A {
public:
std::unique_ptr<B> var;
A() {
std::vector<int> someVector;
var = std::make_unique<B>(5, 2, someVector);
}
};
int main() {
A foo();
return 0;
}
should do the trick.

Derived method in not called in base class

I don't know how to put a right title. sorry by my problem is very hard with me.
I have a class myCal.h:
class myCal
{
public:
myCal();
int add(int a, int b);
int sub(int a, int b);
int expresstion(int a, int b, int c);
};
and myCal.cpp:
myCal::myCal()
{
}
int myCal::add(int a, int b)
{
return a+b;
}
int myCal::sub(int a, int b)
{
return a-b;
}
int myCal::expresstion(int a, int b, int c)
{
return add(sub(a, b), c);
}
in main.cpp, i have class mockcal like this:
class mockcal : public myCal
{
public:
int sub(int a, int b)
{
return 100;
}
int expresstion(int a, int b, int c)
{
return myCal::expresstion(a,b,c);
}
};
if I run myCal.expresstion(3,2,1), the return value is 2, that's OK,
but when I run mockCal.expresstion(3,2,1), the return values still is 2, I want it return 101.
please help me to do this, but do not change anything in mockCal::expresstion.
Thanks so much.
You need to make the method int sub(int a, int b) virtual in the base class ( myCal class in your code), if you want to override it in the mockcal class
class myCal
{
public:
//...
virtual int sub(int a, int b);
//..
};

c++ nested class with 1 of the object as private

I have two classes, for example class A and B. B is encapsulation in A under private;
class A
{
private:
int x;
int y;
B b;
public:
void set(int , int, int, int, int);
void setX(int);
void setY(int);
string toString();
};
void A::set(int high, int low, int middle)
{
B(high, low, middle);
setX(x);
setY(y);
}
void A:: setX(int x)
{
this -> x = x;
}
void A:: setY(int y)
{
this -> y = y;
}
string A::toString()
{
string str;
ostringstream convert;
convert << getlow();
str = convert.str();
return str;
}
class B
{
private:
int low;
int middle;
int hight;
public:
B();
B(int, int, int);
int getLow();
int getMiddle();
int getHigh();
};
in another class with my int main
int main ()
{
int test1,test2,test3,test4,test5;
// with lots of codes
A a;
a.set (test1,test2,test3,test4,test5);
}
When i get some values from int main, i pass in the 3 values into set, which initialize object B. when i use the getB function, all i get is values of 0, or it doesn't appear at all. (I have a constructor that takes in arguments and set all int to 0). Can someone enlighten me? And please do not tell me not to use this. I'm new to C++ so guide me along.
Change the definition of class A at least the following way
class A
{
private:
B b;
int x;
int y;
public:
void set(int, int, int);
int getB();
};
void A::set(int high, int low, int middle)
{
b = B(high, low, middle);
}
int A::getB()
{
return b.getLow();
}
in your set you don't initialize b member variable:
A::set(int high, int low, int middle)
{
B(high, low, middle);
}

Making a structure in class

I am making my first steps in learning OOP . And here is the first problem which I can't solve.
The max function in this class should return the maximum of two numbers . I want to keep the numbers in the private scope and the functions in the public scope . But when I want to use variables from struct data{} in the public scope the compiler says that the variables are not declared . Please tell me why I get these errors .
class myclass{
private:
struct data{
int q ;
int w;
};
public:
void get(int a, int b){
struct data = {a , b}; // here I want to pass the variables to data struct
}
int max (){ // this function returns the biggest number
if(q>w)
return q;
else
return w;
}
};
struct data{
int q ;
int w;
};
only declares a type, not an object, so there are no q and w members anywhere inside your class instances. You need the declare an instance of the struct:
struct {
int q;
int w;
} data;
Then, you can write max as:
int max()
{
if (data.q > data.w)
return data.q;
else
return data.w;
}
(I've no idea what your get method is supposed to do, so I have no replacement for that.)
In C++ "class" and "struct" are close to being synonymous (the same thing). The ONLY difference is that a "struct" defaults to being "public" accessibility while a "class" defaults to private.
Once you understand this, it should become obvious that what you are doing is defining a sub-type within your class.
class myclass {
private: // <- not required, you already said that by saying "class".
struct data {
// <-- this is a class definition with "public:" just here.
...
};
};
C++ allows you to nest class/structure definitions so that you can, for example, create structures that marshal parameters or return values.
class Database {
class Result { ... };
};
...
class Exam {
class Result { ... };
};
These two result classes avoid namespace collision, by being Database::Result and Exam::Result instead of just "Result".
However - these are only definitions. They do not - as shown - have any effect on the outlying class, that is: they aren't being used to add a member to the class.
Your code:
class myclass{
private:
struct data{ // <-- this is a TYPE declaration, struct myclass::data
int q ; //
int w; //
}; // <-- no member name here so does not affect myclass itself.
public:
void get(int a, int b){
struct data = {a , b}; // here I want to pass the variables to data struct
}
int max (){ // this function returns the biggest number
if(q>w)
return q;
else
return w;
}
};
Declares a type "myclass::data" but does not add a member of type "myclass::data" to the class. The line "struct data = " is illegal, you're trying to assign values to a TYPE.
It should probably be written as
class MyClass {
int m_q;
int m_w;
public:
void set(int q, int w) {
m_q = q;
m_w = w;
}
int max() const {
return (m_q > m_w) ? m_q : m_w;
// or #include <algorithm> and return std::max(m_q, m_w);
}
};
You only need to hoist q & w into a struct if you are going to reuse that structural definition outside the confines of the class, e.g. in derived or parallel classes where you may want to add more of the same type of thing, in which case, you could perhaps do the following, but if you do it this exact way you'll eventually kick yourself for breaking encapsulation:
class MyClass {
public:
struct Data {
int m_q;
int m_w;
};
private:
Data m_data;
void set(int q, int w) {
m_data.m_q = q;
m_data.m_w = w;
}
int max() const {
return (m_data.m_q > m_data.m_w) ? m_data.m_q : m_data.m_w;
}
};
A better way, if this coupling of members needs to be externally visible to some degree would be:
class MyClass {
public:
class Data {
int m_q;
int m_w;
public:
Data() : m_q(0), m_w(0) {}
Data(int q, int w) : m_q(0), m_w(0) {}
void set(int q, int w) {
m_q = w;
m_w = w;
}
int q() const { return m_q; }
int w() const { return m_w; }
int max() const { return (m_q > m_w) ? m_q : m_w;
};
private:
Data m_data;
public:
MyClass() : m_data() {} // or = default
MyClass(int q, int w) : m_data(q, w) {}
MyClass(const Data& data) : m_data(data) {}
// Read-only access
const Data& data() const { return m_data; }
// To allow write access, e.g. for set:
Data& data() { return m_data; }
};
It's kinda overkill for such a simple case, but welcome to C++: the boilerplate language.
You have defined the structure but there is no object of that type. You should declare an object and you will not get any error.
class myclass{
private:
struct data{
int q ;
int w;
}var;
public:
void get(int a, int b){
var .q= a;
var.w=b; // here I want to pass the variables to data struct
}
int max (){ // this function returns the biggest number
if(var.q>var.w)
return var.q;
else
return var.w;
}
};