The following code does not print "Destructor is called". Why? However, my book prints it out. How to print the destructor statement? Please suggest.
#include <iostream>
using namespace std;
class HelloWorld
{
public:
//Constructor
HelloWorld()
{
cout<<"Constructor is called"<<endl;
}
//Destructor
~HelloWorld()
{
cout<<"Destructor is called"<<endl;
}
};
int main()
{
//Object created
HelloWorld obj;
system("PAUSE");
return 0;
}
Try this:
int main()
{
{ // start a scope
//Object created
HelloWorld obj;
} // scope ends here, obj will be deleted here
// now pause so you can see the output before program ends.
system("PAUSE");
return 0;
}
Related
#include <iostream>
#include <pthread.h>
using namespace std;
class Base
{
private:
public:
void threadCall1( void * value)
{
cout<<"inside threadCall1"<<endl;
cout<<"Value is"<<(int *)value<<endl;
}
protected:
};
class Derived
{
private:
public:
void threadCall2 ();
protected:
};
void *passValue(void * q)
{
cout<<"inside passValue"<<endl;
Base *b = new Base();
b->threadCall1(q);
cout<<"after threadCall1"<<endl;
Derived *d;
cout<<(int *)q<<endl;
pthread_exit(NULL);
}
void Derived::threadCall2()
{
cout<<"inside threadCall2"<<endl;
}
int main ()
{
int k = 2;
pthread_t t1;
cout<<"inside main"<<endl;
pthread_create(&t1,NULL,&passValue,(void *)k);
cout<<"after pthread_create"<<endl;
return 0;
}
Output:
inside main
after pthread_create
Everything seems fine but don't know why passValue is not getting called and I get the above output but other logs namely inside passValue is missing
Your main terminates early and kills the thread immediately after creation. Add this line before return 0; of the main:
pthread_join(t1, NULL);
This will make the main thread wait for (blocks) for the termination of t1.
I have created a program in C++ for a class, and one of the requirements is to output a string when certain parts of the program have been called. For most of these I have simply assigned a string to a member variable and then outputted that variable. I wanted to know is it possible for me to assign the string in a destructor and then output that string? When I try it, it outputs nothing.
ie:
Class
private:
string output;
~Class {
output = "destructor has fired!";
}
int main(){
cout << class.message;
}
This is pseudocode so please ignore syntax mistakes/missing pieces.
It certainly is possible to output a message in the destructor, to know that it has fired, and one way to do it is this...
#include <iostream>
#include <string>
using namespace std;
class C{
string output; // by default private
public:
C(){}
~C() { cout << output << endl; }
void setString(const string& s) {
output = s;
}
};
int main()
{
{
C s;
s.setString("Destructor has fired");
}
return 0;
}
If I understand your question right, this is what you are expected to do. Note: no member variable, direct calls to std::cout.
#include <iostream>
#include <string>
using namespace std;
class C{
public:
C() {
cout << "C ctor" << endl;
}
~C() {
cout << "C dtor" << endl;
}
};
int main()
{
{
C s;
}
return 0;
}
I have searched but still didn't get easy and proper answer, Below is my code.
#include <iostream>
using namespace std;
class Parent
{
private:
int a;
public:
Parent():a(3) { cout << a; }
};
int main()
{
Parent obj;
return 0;
}
Can you add additional lines of code that can prove or show me that initializer list call before constructor?
I would modify you code ever so slightly:
#include <iostream>
using namespace std;
class Parent
{
public:
int a;
public:
Parent():a(3){
a = 4;
}
};
int main()
{
Parent obj;
cout << obj.a;
return 0;
}
The output is 4, thus a was initialized with 3 and then assigned 4.
Simply add data member, which has constructor, which prints something. Example:
#include <iostream>
using namespace std;
struct Data {
Data(int a) {
cout << "Data constructor with a=" << a << endl;
}
};
class Parent
{
private:
Data a;
public:
Parent():a(3){
cout << "Parent constructor" << endl;
}
};
int main()
{
Parent obj;
return 0;
}
Output:
Data constructor with a=3
Parent constructor
Conclusion: Data constructor was called before constructor body of Parent.
You don't get variable "a" value 10 in this program , a assigned before constructor method called .
#include<iostream>
using namespace std;
class Test
{
public:
int a,b;
public:
Test():a(b){
b=10;
}
};
int main()
{
Test obj;
cout<<"a :"<<obj.a<<" b:"<<obj.b;
return 0;
}
This is best shown with multiple classes:
#include <iostream>
class A
{
public:
A()
{
std::cout << "Hello World!" << std::endl;
}
};
class B
{
private:
A* a;
public:
// Call a's constructor
B():a(new A)
{
// Some code
}
~B()
{
delete a;
}
};
int main()
{
B obj;
return 0;
}
Why doesn't this code print the letter a?
#include <iostream>
#include <stack>
void a()
{
std::cout<<"a";
}
int main ()
{
void a();
return 0;
}
You're accidentally declaring a function inside main() instead of calling it.
int main ()
{
void a(); // <-- DECLARES a function, does not call it
return 0;
}
Here is the fix:
int main ()
{
a();
return 0;
}
Also note that you probably want a newline:
void a()
{
std::cout<<"a\n";
}
Or you can use std::endl, if you like typing.
You're declaring your function twice:
#include <iostream>
#include <stack>
void a()
{
std::cout<<"a";
}
int main ()
{
void a(); // this is a declaration
return 0;
}
Do this instead:
int main ()
{
a(); // this is a function call, which will execute your function
return 0;
}
Its ok qwertyu uytrewq, everyone have these kind of problems in start, the main thing is never hesitate to question.
The main error in your code is, that you are declaring the function but not calling it. There are three phases of functions.
Declaration i.e
void a();
Definition i.e
void a ()
{
std::cout << "a";
}
Calling a Function i.e
a();
Now the major error in your program belongs to the 3rd phase (Calling a Function) you are mentioning the function type also which is know as Deceleration, so the correct code is as following.
#include <iostream>
#include <stack>
void a()
{
std::cout<<"a";
}
int main ()
{
a();
return 0;
}
In your main function:
int main()
{
// Don't add Void
a();
return 0;
}
I am trying to call the function hello which belongs to the class program1
#include <iostream>
using namespace std;
class program1
{
program1();
~program1();
hello();
}
program1::hello()
{
cout<<"hello";
}
int main()
{
program1.hello(); //Call it like a normal function...
cin.get();
}
Names inside a class are private by default.
class program1 {
public:
program1();
~program1();
void hello() ;
};
// ...
int main(int, char **) {
program1 myProgram;
myProgram.hello();
return 0;
}
Alternatively, you can invoke a method on a temporary:
int main(int, char **) {
program1().hello();
return 0;
}
but that's probably for later in the semester.
you forgot to create an object:
int main()
{
program1 p1;
p1.hello();
}
Class definition should end with ;
Secondly, you need to instantiate class to call members on it. ( i.e., creation of an object for the class )
In C++, methods should have return type.
program1::hello(); // method should have a return type.
class members and methods are private by default, which means you cannot access them outside the class-scope.
So, the class definition should be -
class program1
{
public: // Correction 1
program1();
~program1();
void hello(); // Correction 2
};
void program1::hello() // Need to place return type here too.
{
cout<<"hello";
}
Now on creation of object for class program1, it's method hello() can be called on it.
This version is edited. (make sure you include all the body of the methods)
#include <iostream>
using namespace std;
class program1
{
public: // To allow outer access
program1();
~program1();
void hello(); // The void
}; // Don't miss this semicolon
void program1::hello() // The void
{
cout<<"hello";
}
int main()
{
program1 prog; // Declare an obj
prog.hello(); //Call it like a normal function...
cin.get();
}
I noticed that you left out return type for your hello() function.
If you want to call hello() as a member function, then as suggested you should create an object to it.
program1 prog;
prog.hello();
If you want to call it without an object, the you should use static function.
class program1
{
public: // To allow outer access
program1();
~program1();
static void hello(); // The void
}
then you can call it this way:
program1::hello();
Therefore the working code should be this way:
#include <iostream>
using namespace std;
class program1 {
public:
void hello();
}; // Don't miss this semicolon
class program2 {
public:
void static hello(); // to demonstrate static function
}; // Don't miss this semicolon
void program1::hello() {
cout << "Hello, I'm program1." << endl;
}
void program2::hello() {
cout << "Hello, I'm program2." << endl;
}
int main(void) {
program1 prog1;
prog1.hello(); // Non-static function requires object
program2::hello(); // Static function doesn't
return 0; // Return 0
}