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

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);
}

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);

Multiple Type output in Polymorphism

I have a problem when i want to create a pure virtual function and assign to this multiple range of output formats (like int, double and char).
i wrote below code and it works only when i eliminate my abstract class which it's not thing that i want.
my code:
enter code here
//class DynamicArray
//{
// public:
// virtual void GetData(unsigned int s, int& d) = 0;
// virtual void SetData(unsigned int s, int& d) = 0;
//};
class DynamicArrayDouble//: public DynamicArray
{
private:
unsigned int m_Length;
double* arr;
public:
DynamicArrayDouble(unsigned int l)
{
m_Length = l;
arr = new double[m_Length];
}
void SetParam(unsigned int l)
{
m_Length = l;
arr = new double[m_Length];
}
void GetData(unsigned int s, double& d)
{
d = arr[s];
}
void SetData(unsigned int s, double& d)
{
arr[s] = d;
}
};
when i uncomment DynamicArray class and DynamicArrayDouble inherit it i face with some error. it should be noted, first time i try to use void* for second parameter for Set and Get methods, but again i receive some errors that i can't use this code style like this:
error: cannot declare variable 'd1' to be of abstract type 'DynamicArrayDouble'
and code of above error is:
class DynamicArray
{
public:
virtual void GetData(unsigned int s, void* d) = 0;
virtual void SetData(unsigned int s, void* d) = 0;
};
class DynamicArrayDouble: public DynamicArray
{
private:
unsigned int m_Length;
double* arr;
public:
DynamicArrayDouble(unsigned int l)
{
m_Length = l;
arr = new double[m_Length];
}
void SetParam(unsigned int l)
{
m_Length = l;
arr = new double[m_Length];
}
void GetData(unsigned int s, double* d)
{
*d = arr[s];
}
void SetData(unsigned int s, double* d)
{
arr[s] = *d;
}
};
int main()
{
DynamicArrayDouble d1(5);
double x=0;
for(unsigned int i=0;i<5;i++)
{
x = ((i+1.0)/2);
d1.SetData(i,&x);
}
for(unsigned int i=0;i<5;i++)
{
d1.GetData(i,&x);
cout << "Data " << i+1 << " is = " << x << endl;
}
return 0;
}
i write my codes in codeblocks.
i will appreciate your answer... Thank you.
You can't simply override
void func(some_pointer_type ptr);
with
void func(some_other_pointer_type ptr);
and same goes for references. Those are in fact considered to be completely unrelated if they don't match.
DynamicArrayDouble - this name tells me you should look into templates and not write the same code for all the types you'll need. This is how STL works. You'll completely avoid runtime polymorphism.
For starters:
template <typename T>
DynamicArray;

Using nested class in head class constructor

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);
}

class inheritance and using setters and getters

Here I've made a derived class called Essay, from the base class GradedActivity. I've made an object of the Essay class in main called object. When I wrote object.setGrammar(grammarPts) in main(), I'd hoped to feed what the score is to be held in the variable grammar in the setGrammar() function. What am I doing wrong? Thanks!
I get one error:
99 8 F:\lab6part3.cpp [Error] request for member 'setGrammar' in 'object', which is of non-class type 'Essay(float, float, float, float)'
#include <iostream>
using namespace std;
//class gradedactivity (page 900)
class GradedActivity
{
protected:
double score;
public:
//default constructor
GradedActivity()
{
score = 0.0;
}
//parameterized constructor
GradedActivity(double s)
{
score = s;
}
setScore(double s)
{
score = s;
}
double getScore() const
{
return score;
}
char getLetterGrade() const;
};
class Essay : public GradedActivity
{
private:
float grammar;
float spelling;
float length;
float content;
public:
Essay(float g, float s, float l, float c)
{
setGrammar(g);
setSpelling(s);
setLength(l);
setContent(c);
}
void setGrammar(float);
float getGrammar();
void setSpelling(float);
float getSpelling();
void setLength(float);
float getLength();
void setContent(float);
float getContent();
};
void Essay::setGrammar(float g)
{
grammar = g;
}
float Essay::getGrammar() {return grammar;}
void Essay::setSpelling(float s)
{
spelling = s;
}
float Essay::getSpelling() {return spelling;}
void Essay::setLength(float l)
{
length = l;
}
float Essay::getLength() {return length;}
void Essay::setContent(float c)
{
content = c;
}
float Essay::getContent() {return content;}
int main()
{
float grammarPts;
cout << "How many points, out of 30, did the student get for grammar?";
cin >> grammarPts;
Essay object;
object.setGrammar(grammarPts);
return 0;
}
This could just be because you never defined a default constructor for Essay.
Anyway, I defined a default constructor and your code runs fine so that might be the issue. https://ideone.com/yNxV8N

Can some one help me with this error?

I have to write this program as an assignment. I've been struct with the following error for a week now.Thank in advance.
========Error list==============
1. x,w,y is undefined.
2. For checkin/checkout it says "declaration is in compatible with void employee::checkin/checkout(int,int,int)"
3. In this->Time(a,b,c) it's said type name is not allowed.
P.S. If there is anyway to improve it or something need fixing please tell me that would be very great. Thanks again in advance.
#include <iostream>
#include <iomanip>
#define N 2
using namespace std;
class Time
{
private:int h,m,s;
public:
void time_diff(Time T1,Time T2)
{
Time t;
t.s=(60+T2.s-T1.s)%60;
t.m=(60+T2.m-T1.m+(1-(60+T2.s-T1.s)/60))%60;
t.h=T2.h-T1.h+(1-(60+T2.m-T1.m+(1-(60+T2.s-T1.s)/60))/60);
t.h=t.h<0?t.h+=24:t.h;
*this=t;
}
Time(int hh=0,int mm=0,int ss=0){
s=ss%60;
m=(mm+ss/60)%60;
h=(hh+(mm+ss/60)/60)%24;
}
void set_time(int a,int b,int c){
this->Time(a,b,c);
}
double converter(int hn,int m,int s)
{
int hn,mn,sn;
hn=h*3600;
mn=m*60;
sn=hn+mn+s;
return sn;
}
};
class employee
{
int id;
protected:
Time t_in,t_out;
double wage;
public:
employee(int=0,double=5);
void checkin(int,int,int);//set t_in
void checkout(int,int,int);//set t_out
void display();//display id and total time at work (t_out-t_in )
};
employee::employee(int w,double x)
{
x=wage;
w=id;
}
class staff:public employee{
public:
double get_paid();//wage*(t_out-t_in);
staff(int=0,int=5);//id,wage
};
void employee::checkin()
{
t_in.set_time(x,w,y);
}
void employee::checkout()
{
t_out.set_time(x,w,y);
}
double staff::get_paid()
{
int paid,load;
Time wo;
wo.time_diff(t_out,t_in);
load=wo.converter;
paid=load*wage;
}
If you really need set_time, try this::
class Time
{
...
void set_time(int a, int b, int c) {
*this = new Time(a, b, c);
}
...
}
... however I think set_time is useless because you have the constructor already.
In checkin/checkout implementation you have to repeat the same firm of the declaration, so:
void employee::checkin(int x, int w, int y)
{
t_in.set_time(x, w, y);
}
void employee::checkout(int x, int w, int y)
{
t_out.set_time(x, w, y);
}
Indeed the constructor employee is wrong because you set local arguments x and w with the same values of member fields wage and id but these values are zeros and remain so.
You would probably like do the inverse, so:
employee::employee(int w, double x)
{
wage = x;
id = w;
}