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
Related
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);
I'm trying to make this code work:
#include <iostream>
using namespace std;
int f(int x) {
return x+1;
}
class A {
public:
int g(int y);
};
int A::g(int y) = f;
int main() {
A test;
cout << test.g(3) << endl;
return 0;
}
It does not compile because of the line int A::g(int y) = f;.
What is the correct way to achieve that an external function can be used as a method?
You can use a pointer to function as a member of class A. Now assign function f to g member of A.
int f(int x) {
return x+1;
}
class A {
public:
int (*g)(int);
};
int main(){
A test;
test.g = f;
cout << test.g(10); // prints 11
}
You can accomplish the same by making your function a callable objects by implementing () operator. so you can have that as member of the class, and then it can normally be used as function on the class objects.
#include <iostream>
struct do_something{
int operator()(int num){
return num;
}
};
class test{
int sum;
public:
do_something fun;
};
int main(){
test obj;
std::cout << obj.fun(10);
}
Why I am not getting output. I was expecting fun(Test2) called as output. As the parameters passed are different for the function so there shouldn't be any conflict.
#include <iostream>
using namespace std;
class Test2
{
int y;
};
class Test
{
int x;
Test2 t2;
public:
operator Test2 () { return t2; }
operator int () { return x; }
};
void fun ( int x) { cout << "fun(int) called"; }
void fun ( Test2 t ) { cout << "fun(Test 2) called"; }
int main()
{
Test t;
fun(t);
return 0;
}
Your call to fun() is ambiguous. t is of type Test, which can be converted to both Test2 and int, hence both fun implementations are candidates.
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_} { }
};
Here is a complete example.
I want to forbid using A::set from objects casted from B to A by allowing only casting
B to const A.
How to do it?
(I can't use virtual functions)
#include <iostream>
#include <cassert>
using namespace std;
class A {
public:
int get() const { return i_; }
void set(int i) { i_ = i; }
protected:
int i_;
};
class B : public A {
public:
int ok() const { return A::get() == copy_i_; }
void set(int i) { A::set(i); copy_i_ = i; }
protected:
int copy_i_;
};
void test2() {
A a;
a.set(3); // ok here
cout << a.get() << endl;
B b;
b.set(5);
A& aa = b;
assert(b.ok());
aa.set(3); // not ok here
assert(b.ok()); // fail-here
}
int main() {
test2();
return 0;
}
You could make the inheritance private and provide a member function in B to use instead of casting.
const A& B::convert_to_A() const { return *this; }
Why casting? Making void A::set(int i) protected will work in your case.
There is no need for forbidding non-const casts. You can solve your problem by using the template method design pattern.
#include "stdafx.h"
#include <iostream>
#include <cassert>
using namespace std;
class A {
public:
int get() const { return i_; }
void set(int i) { assert(i_ = i); copy_i();}
protected:
int i_;
virtual void copy_i(){};
};
class B : public A {
public:
int ok() const { return A::get() == copy_i_; }
protected:
int copy_i_;
void copy_i(){copy_i_ = i_; }
};
void test2() {
B b;
b.set(5);
A& a = b;
assert(b.ok());
a.set(3);
assert(b.ok()); // success!
}
int main() {
test2();
return 0;
}