1. To define a function outside the class, there MUST be a prototype inside the class.
For eg.
class Jg{
int num;
public:
Jg(){ num=0; }
};
void Jg::Display() //Error will be here
{ cout<<num;
}
//This Program will give the error "no 'void Jg::Display()' member function declared in class 'Jg'"
2. What if one member function defined twice, once inside class and once outside?
Will give error "[error]redefintion of int Jg::Display()"
And then pointing on the first defintion it will give "[NOTE] int Jg::Display() previously defined here"
PROGRAM - #include<iostream>
using namespace std;
class Jg{
int num;
public:
Jg(){
num=0;
}
Display(){
cout<<"Hello";
}
};
int Jg::Display(){ //ERROR
cout<<num;
}
main(){
Jg J1;
J1.Display();
}
3. What if a member function declared twice inside the class?
Will show error "int Ag::Display() cannot be overloaded"
Program- #include<iostream>
using namespace std;
class Ag{
int num;
public:
Ag(){
num=0;
}
Display(){
cout<<"Hello";
}
int Display(){ //ERROR
cout<<num;
}
};
main(){
Ag A1;
A1.Display();
}
4. What if member function defined once above the main() function, and the other below the main() function?
Will give error "redefintion of int Ag::Display()"
PROGRAM - #include<iostream>
using namespace std;
class Ag{
int num;
public:
Ag(){
num=0;
}
Display();
};
int Ag::Display(){
cout<<"Hello"; }
main(){
Ag J1;
J1.Display();
}
int Ag::Display(){
cout<<num; }
5. What if no main() function?
Dev C++ shows the error "undefined reference to WinMain@16"
and also "ld returned 1 exit status"
6. What if i dont give the return type for a member function? (like we dont give return type for constructor)
It will automatically have 'int' return type
For eg. class Jg{
int num;
public:
Jg(){ num=0; }
Display(); //return type not provided
};
int Jg::Display() //NO ERROR
{ cout<<num;
}
7. What if return type not provided for a global function?
Ans. Default is 'int' return type
8. What if we write just 'main()' instead of 'int main()' or 'void main()'?
Ans. Everything's still fine, default return type in modern compilers is 'int'
9. What if return statement (ie. return 1; etc) not provided in main()?
Ans.Default return value is 0
But in case any error occurs the return code for that error will be printed (this will happen even if you have given a return statement!)
10. Can we just write the defintion of a funtion without the {} brackets ?
Ans. No [applies to main() funtion as well as all others] . See this for example,
Program 1: #include<iostream>
say()
std::cout<<"Hello";
main(){ say(); }
Program 1 will give the errors-
[Error] expected constructor, destructor, or type conversion before 'std' (line 3)
[Error] 'pri' was not declared in this scope (at line 4)
NOTE- As most of you may already know, that the same can be done in loops, for eg while(1) cout<<"Hello";
11. Are spaces allowed in #include directive?
No,
#include< iostream > //will not find <iostream> //From Bjarne Stroustrup's Book (4th Book)
//I use Dev C++ Ver 5.11 (IDE)...wrote it since there may be difference in other IDEs
For eg.
class Jg{
int num;
public:
Jg(){ num=0; }
};
void Jg::Display() //Error will be here
{ cout<<num;
}
//This Program will give the error "no 'void Jg::Display()' member function declared in class 'Jg'"
2. What if one member function defined twice, once inside class and once outside?
Will give error "[error]redefintion of int Jg::Display()"
And then pointing on the first defintion it will give "[NOTE] int Jg::Display() previously defined here"
PROGRAM - #include<iostream>
using namespace std;
class Jg{
int num;
public:
Jg(){
num=0;
}
Display(){
cout<<"Hello";
}
};
int Jg::Display(){ //ERROR
cout<<num;
}
main(){
Jg J1;
J1.Display();
}
3. What if a member function declared twice inside the class?
Will show error "int Ag::Display() cannot be overloaded"
Program- #include<iostream>
using namespace std;
class Ag{
int num;
public:
Ag(){
num=0;
}
Display(){
cout<<"Hello";
}
int Display(){ //ERROR
cout<<num;
}
};
main(){
Ag A1;
A1.Display();
}
4. What if member function defined once above the main() function, and the other below the main() function?
Will give error "redefintion of int Ag::Display()"
PROGRAM - #include<iostream>
using namespace std;
class Ag{
int num;
public:
Ag(){
num=0;
}
Display();
};
int Ag::Display(){
cout<<"Hello"; }
main(){
Ag J1;
J1.Display();
}
int Ag::Display(){
cout<<num; }
5. What if no main() function?
Dev C++ shows the error "undefined reference to WinMain@16"
and also "ld returned 1 exit status"
6. What if i dont give the return type for a member function? (like we dont give return type for constructor)
It will automatically have 'int' return type
For eg. class Jg{
int num;
public:
Jg(){ num=0; }
Display(); //return type not provided
};
int Jg::Display() //NO ERROR
{ cout<<num;
}
7. What if return type not provided for a global function?
Ans. Default is 'int' return type
8. What if we write just 'main()' instead of 'int main()' or 'void main()'?
Ans. Everything's still fine, default return type in modern compilers is 'int'
9. What if return statement (ie. return 1; etc) not provided in main()?
Ans.Default return value is 0
But in case any error occurs the return code for that error will be printed (this will happen even if you have given a return statement!)
10. Can we just write the defintion of a funtion without the {} brackets ?
Ans. No [applies to main() funtion as well as all others] . See this for example,
Program 1: #include<iostream>
say()
std::cout<<"Hello";
main(){ say(); }
Program 1 will give the errors-
[Error] expected constructor, destructor, or type conversion before 'std' (line 3)
[Error] 'pri' was not declared in this scope (at line 4)
NOTE- As most of you may already know, that the same can be done in loops, for eg while(1) cout<<"Hello";
11. Are spaces allowed in #include directive?
No,
#include< iostream > //will not find <iostream> //From Bjarne Stroustrup's Book (4th Book)
//I use Dev C++ Ver 5.11 (IDE)...wrote it since there may be difference in other IDEs
Comments
Post a Comment
You are free to leave any kind of suggestion, or improvement. It doesn't require you to sign in either.