Hi,

Please tell me in the following question why no copy constructor is called when fun returns the A object.
While returning b and constructing c, there is no copy constructor called. Why??

#include<iostream>

using namespace std;

class A
{
    public:
    A(){cout <<"Default\n";}
    A(const A&){
                cout<<"copy\n";
    }
    A fun(A a)
        {
             A b; 
            return b;
        }
};

int main()
{
    A a;
    A c = a.fun(a); // Here atleast copy copy constructor should have been called.
    return 0;
}

I didn't want to change/improve the program, just wanted to know how the call chain is going on.
Please tell me.

Hi All,

Why the output of the following program is 0231??
Please resolve my doubt.

#include <iostream>

using namespace std;
    class B
    {
        int data;
    public:
        B() { data =0;}
        int func(){cout<<data++;return data++;      }
    };

int main()
{
    B b;
    cout<<b.func()<<b.func()<<endl;
    return 2;
}

Hi All,
Pleas tell me why the following program is giving 35 as output??
if fun() returns int type then it is compilation error. why?

#include<iostream>
using namespace std;

int &fun()
{
    static int x = 10;
    return x;
}
int main()
{
    fun() = 30;
    cout << fun();
    return 0;
}

Hi All

Can somebody clear my query that why the second operand of '+' operator is called first in the code below?
When operator '+' takes left to right.

#include <iostream>
using namespace std;

class A
{
    int i;
        public:
        A(int i=-1){cout<<"A const i = "<< i <<endl;}

        int operator+(const A &a)
        {
            cout<<"operator + "<<endl;
            return 1;
        }
};

int main()
{
        A(10) + A(18); // A(18) is constructed first !! Why??
        return 0;
}

Hi All,

Could you please clear my doubt why this second catch block is called when exception is thrown only once.

#include <iostream>
using namespace std;

class E {
public:
    const char* error;
    E(const char* arg): error(arg) {}
};

class B {
public:
    B() {};
    ~B(){cout<<"~B() called"<<endl;}
};

class D: public B {
public:
    D();
    ~D() { cout<<"~D() called"<<endl; }
};

D::D() try :B(){
    throw E("Exception in D");
} catch(E&e)
{
    cout<<"D()"<<e.error<<endl;
};

int main()
{
    try {
        D val;
    }catch(...) { cout<<"Last"<<endl;}
}

Hi all,

Please answer this question. Why the output is ACA??
In the line obj = obj+6; the 6 should have been converted to obj type but, why it is converting the obj to int type?
Please help...

#include <iostream>

using namespace std;

class Base {
public:
    Base(int m_var=1):i(m_var){
        cout<<"A";
    }
    Base(Base& Base){
        cout<<"B";
        i=Base.i;
    }
    operator int() {
        cout<<"C";
        return i;
    }
    int operator+(Base& Base)
    {
        cout<<"D";
        return i+Base.i;
    }
private:
    int i;
};

int main()
{
    Base obj;
    obj = obj+6;
    return 0;
}

Hi Phorce... The polymorphism you said is different, it is in case when we have classes.
My example is having two global functions (not in any class), with same name and difference in type of parameter.
Please answer if the question is clear.

I didn't get you. the first example has the double 3.14. compiler interpreting the call as foo(int, double);
but why is this happening in the case of Goo call?

How come foo(3,3.14); Doesn't work
but Goo(3,3.14); works?
Please reply.

#include <iostream>
using namespace std;
double foo(double a, double b) {
    cout<<"foo(double a, double b)"<<endl;
}
 int foo(int a, int b){
    cout<<"foo(int a, int b)"<<endl;
 }
double Goo(float a, float b){
    cout<<"Goo(float a, float b)"<<endl;
}
  int Goo(int a, int b){    
    cout<<"Goo(int a, int b)"<<endl; //this one
  }

int main()
{
    foo(3,3.14); // Doesn't work
    Goo(3,3.14); //Works
}

How come the output of this program is 1413, I expected it to be 1414.
(i + 1)->value() should have called R::value(). is n't it?

#include<iostream>
#include<vector>
using namespace std;

class Q
{
public:
    Q(int n = 0) : m_n(n) { }
    virtual int value() const { return m_n; }
    virtual ~Q() { }
protected:
    int m_n;
};

class R : public Q
{
public:
    R(int n = 0) : Q(n) { }
    virtual int value() const { return m_n + 1; }
};
int main(void)
{
    const Q a(1);
    const R b(3);
    const Q *x[2] = { &a, &b };
    typedef std::vector<Q> V;
    V y;
    y.push_back(a);
    y.push_back(b);
    V::const_iterator i = y.begin();
    std::cout << x[0]->value() << x[1]->value()
              << i->value() << (i + 1)->value() << std::endl;
}

I want to learn multithreading in C++. somewhere on internet I got a piece of code using popen() function of stdio.h. I just wanted to know that if this function helps in creating the new threads or it doesn't come under multithreading.

Please guide me in multithreading.

Hi everyone,

I am expert in C++ and want to be part of this discussion forum.

Floats and doubles can't be compared using relational operators. That may be the issue. you can search web for how to compare floats.

[QUOTE=harsha_123;1728940]I'm new to c++. I was studying data structures and came to know Linked Lists Lately.
We had a problem of finding an item stored inside a linked list and here's the code I wrote using Visual Studio to store and find an item. This code gives me a run time error.

[COLOR="Red"]1. "Link.h" file[/COLOR]

[CODE]

pragma once

class Link
{
public:
double num;
char nam[20];
Link* next;
Link(double pnum,char pnam[]);
void displayLink();
};

[/CODE]

[COLOR="Red"]2. "Link.cpp" file[/COLOR]

[CODE]

include "StdAfx.h"

include "Link.h"

include<iostream>

include<cstring>

using namespace std;

Link::Link(double pnum,char pnam[])
{
next = NULL;
num = pnum;
strcpy(nam,pnam);
}

void Link::displayLink()
{
cout<<num<<"\t"<<nam<<endl;
}

[/CODE]

[COLOR="Red"]3. "LinkList.h" file[/COLOR]

[CODE]

pragma once

include"Link.h"

class LinkList
{
private:
Link *first;

public:
LinkList();
void insertFirst(double n,char nm[]);
Link* find(double n);
};

[/CODE]

  1. "Linklist.h" file

[CODE]

include "StdAfx.h"

include "LinkList.h"

include"Link.h"

include<iostream>

using namespace std;

LinkList::LinkList()
{
first = NULL;
}

void LinkList::insertFirst(double n,char nm[])
{
Link *temp = new Link(n,nm);
temp->next = first;
first = temp;
cout<<"DATA ENTERED"<<endl;
}

Link LinkList::find(double n)
{
Link
cur = first;
while(cur!=NULL)
{
if(cur->num == n)
{
return cur;
}
else
{
cur = cur->next;
}
}
return NULL;

}

[/CODE]

[COLOR="Red"]5."LinkList_5.cpp" (main program)[/COLOR]

[CODE]

include "stdafx.h"

include<iostream>

include"Link.h"

include"LinkList.h"

using namespace std;

int _tmain(int argc, _TCHAR argv[])
{
LinkList
ne = new LinkList();
ne->insertFirst(12.22,"Charith");
ne->insertFirst(13.22,"Lalitha");
cout<<ne->find(12.22)->num;

system("PAUSE");
return 0;

}

[/CODE] ...