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);
Related
For the following code, Fun() is returning a temporary object of Test type
#include<iostream>
#include<typeinfo>
using namespace std;
class Test
{
private:
int x;
static int count;
public:
Test(int i=0 ) {cout<<"sumit";}
Test(const Test& rhs) : x(rhs.x) { ++count; cout<<"sum"; }
static int getCount() { return count; }
};
int Test::count = 0;
Test fun()
{
Test ob(7);
return ob;
}
int main()
{
Test a=fun();
cout<<typeid(fun()).name();
cout<< Test::getCount();
return 0;
}
Still copy is not taking place
The output of the code:
constructor 4Test
Program works but I am not sure what is wrong with constructor since every time program runs it gets this error "warning: base class 'Alat' is uninitialized when used here to access 'Alat::ime' [-Wuninitialized]". I suppose it's something wrong how I called a constructor from base class but I am not sure what is problem. Really need help, tnx in advance.
#include <iostream>
#include <string>
using namespace std;
class Alat{
protected:
string ime;
int serBr;
int cena;
public:
void setIme(string i);
string getIme();
void setSerBr(int sb);
int getSerBr();
void setCena(int c);
int getCena();
Alat();
Alat(string i, int sb, int c)
:ime(i),
serBr(sb),
cena(c)
{}
void info();
~Alat();
};
#include "Alat.h"
class Rucni : public Alat{
protected:
int minGodKor;
public:
Rucni():Alat(ime, serBr, cena) //I think here is problem, is it wrong called?
{}
int getminGodKor();
void setminGodKor(int min);
void info();
~Rucni();
};
Let the child default constructor call the default parent constructor, and create another child constructor with parameters to call the corresponding one of the parent:
#include <string>
using std::string;
class Alat
{
protected:
string ime;
int serBr;
int cena;
public:
void setIme(string i)
{
ime = i;
}
string getIme()
{
return ime;
}
void setSerBr(int sb)
{
serBr = sb;
}
int getSerBr()
{
return serBr;
}
void setCena(int c)
{
cena = c;
}
int getCena()
{
return cena;
}
Alat()
{
}
Alat(string i, int sb, int c) : ime(i), serBr(sb), cena(c)
{
}
~Alat()
{
}
};
class Rucni : public Alat
{
protected:
int minGodKor;
public:
Rucni() // implicit call of the parent default constructor
{
}
Rucni(string i, int sb, int c) : Alat(i, sb, c) // explicit call of the corresponding parent constructor
{
}
int getminGodKor()
{
return minGodKor;
}
void setminGodKor(int min)
{
minGodKor = min;
}
~Rucni()
{
}
};
int main()
{
Rucni r;
return 0;
}
I have something like this:
using namespace std;
class QuadraticPrimeSolution
{
private:
int a;
int b;
int numberOfPrimes;
bool isPrime(int n, set<int> &primeHash);
public:
QuadraticPrimeSolution(int a, int b):a(a),b(b),numberOfPrimes(0){};
void calculateNumberOfPrimes(set<int> &primeHash);
int getNumberOfPrimes(){return numberOfPrimes;}
};
class QuadraticPrimeSolver
{
private:
struct classcomp {
bool operator() (QuadraticPrimeSolution& lhs, QuadraticPrimeSolution& rhs)
{
return lhs.getNumberOfPrimes()>rhs.getNumberOfPrimes();
}
};
set<QuadraticPrimeSolution, classcomp> solutions;
set<int> primeHash;
QuadraticPrimeSolution getMaxSolution();
int a;
int b;
public:
QuadraticPrimeSolver(int a, int b):a(a), b(b){};
void solve();
};
bool QuadraticPrimeSolution::isPrime(int n, set<int> &primeHash)
{
if(primeHash.empty())
{
primeHash.insert(n);
return true;
}
for(auto it= primeHash.begin(); it!= primeHash.end(); it++)
{
if(n%(*it)==0)
{
return false;
}
}
primeHash.insert(n);
return true;
}
void QuadraticPrimeSolver::solve()
{
for(int i=(-1)*a; i<=a; i++)
{
for(int j=(-1)*b; j<=b; j++)
{
QuadraticPrimeSolution aSolution = new aSolution(i,j);
aSolution.calculateNumberOfPrimes(primeHash);
solutions.insert(aSolution);
}
}
}
int main()
{
QuadraticPrimeSolver QPS(0,40);
QPS.solve();
}
Basically what I am trying to do is compute and store each QuadraticPrimeSolution into a hash table in QuadraticPrimeSolver which I can then access later.
My question is, is my comparator implementation correct? Right now compiler is complaining about my comparator, and the following line for inserting into a set.
solutions.insert(aSolution);
Please help!
class building
{
public:
int getPosition() const {return position;};
private:
int height;
int position;
};
class ManyBuildings
{
public:
void populateBuildings(std::vector<std::string> buildings);
private:
class comparePosition {
public:
bool operator () (const building &lhs, const building &rhs) {
return lhs.getPosition() > rhs.getPosition();
}
};
std::set<building, comparePosition> buildings;
};
use set instead of unordered_set
template parameter for set should be a type, not a function
I have error: No matching function for call to 'Goo::Goo()'
This problem is happening to often, can somebady explain to me where do i make mistakes all the time. I How can i overcome this.
Here is the code of the progam:
#include <iostream>
using namespace std;
class Goo{
private:
int a[10];
int n;
public:
Goo(int x){
n=x;
}
Goo(const Goo &g){
this->n=g.n;
for(int i=0;i<g.n;i++){
this->a[i]=g.n;
}
}
Goo operator=(const Goo &g){
this->n=g.n;
for(int i=0;i<g.n;i++){
this->a[i]=g.n;
}
return *this;
}
Goo operator+(const Goo &g){
Goo goo;
for(int i=0;i<g.n;i++){
goo.a[i]=this->a[i]+g.a[i];
}
return goo;
}
friend istream& operator>>(istream &in,Goo &g){
in>>g.n;
for(int i=0;i<g.n;i++){
in>>g.a[i];
}
return in;
}
friend ostream& operator<<(ostream &out,Goo &g){
for(int i=0;i<g.n;i++){
out<<g.a[i]<<" ";
}
return out;
}
};
int main()
{
Goo A,B;
cin>>A>>B;
Goo C=A+B;
cout<<C;
return 0;
}
When you define a custom constructor (among other reasons), the class no longer has a default constructor:
struct Foo {
int x;
};
Foo foo; // OK
struct Foo {
int x;
Foo(int x_) : x{x_} { }
};
Foo foo; // error
You can fix this by either adding a custom default constructor:
struct Foo {
int x;
Foo() { }
Foo(int x_) : x{x_} { }
};
or having at least one constructor with all default parameters:
struct Foo {
int x;
Foo(int x_ = 0) : x{x_} { }
};
Since C++11, you can also force the compiler to emit the default constructor:
struct Foo {
int x;
Foo() = default;
Foo(int x_ = 0) : x{x_} { }
};
My first post here :)
I am having a problem with the following C++ code. I have an ABC class A, and two derived classes B and C. All of them have a static member called id:
using std::cout;
class A
{
private:
friend int bar(A& a);
static const int id = 1;
virtual void foo() = 0;
};
class B : public A
{
private :
friend int bar(A& a);
static const int id = 2;
void foo() { /*Do something*/ }
};
class C : public A
{
private:
friend int bar(A& a);
static const int id = 3;
void foo() { /*Do something*/ }
};
int bar(A& a)
{
return a.id;
}
int main()
{
B b;
C c;
cout << bar(b) << "\n";
cout << bar(c) << "\n";
return 0;
}
I was expecting this code to print out 2 and 3 - rather it prints out 1 and 1 (bar() is always using A::id). What am I doing wrong? Any ideas?
Based on the comments below, this the final code I am using. It works, but would love to hear more thoughts :)
#include <iostream>
using std::cout;
class A
{
private:
virtual void foo() = 0;
};
class B : public A
{
private:
template <typename T>
friend int bar(T& t);
static const int id = 2;
void foo() { /*do something*/ }
};
class C : public A
{
private:
template <typename T>
friend int bar(T& t);
static const int id = 3;
void foo() { /*do something*/ }
};
template <typename T>
int bar(T& t)
{
return t.id;
}
int main()
{
B b;
C c;
cout << bar(b) << "\n";
cout << bar(c) << "\n";
return 0;
}
a.id will be defined at compile-time as A::id. You would need to define a virtual member (non-static) function in class A and have it overridden in B and C to return their respective ids and call this function in bar.
Is there any way to avoid writing int foo() { return id; } for all the derived classes?
Yes, using templates. For example:
template <typename T>
int foo (T& x)
{
return x.id;
}
However, if id is private, this doesn't reduce the code by all that much.