You are completely wrong here.
Another answer:
There is nothing to say you cannot use cin or coutoutside the main() function (for example, you can use them in a function called from main()). However, using them before main() has been called (e.g. in the constructor of a static object) can have disastrous consequences: cin and cout are themselves static objects, and static initialisation order is undefined. Thus you could be calling them before they have been properly constructed.
the code for the box is: # include <iostream> using namespace std; void main () { cout << "*********\n"; cout << "*\t*\n"; cout << "*\t*\n"; cout << "*\t*\n"; cout << "*\t*\n"; cout << "*\t*\n"; cout << "*\t*\n"; cout << "*\t*\n"; cout << "*********\n"; the code for the oval is: # include <iostream> using namespace std; void main () { cout << " ***\n"; cout <<" * *\n"; cout <<"* *\n"; cout <<"* *\n"; cout <<"* *\n"; cout <<"* *\n"; cout <<"* *\n"; cout <<" * *\n"; cout << " ***\n"; the code for the arrow is: # include <iostream> using namespace std; void main () { cout << " * \n"; cout <<" ***\n"; cout <<"*****\n"; cout <<" * \n"; cout <<" * \n"; cout <<" * \n"; cout <<" * \n"; cout <<" * \n"; the code for the diamond: # include <iostream> using namespace std; void main () { cout << " *\n "; cout <<" * *\n"; cout<<" * *\n"; cout<<" * *\n"; cout<<"* *\n"; cout<<" * *\n"; cout<<" * *\n"; cout <<" * *\n"; cout << " *\n "; }
#include #include void main() { long 11; clrscr(); cout<<"size of long="< getch(); }
Yes you can use the same function name for a member function and an external function. They are primarily distinguished by the number and type of arguments they accept (the function signature). If they match exactly, then the scope resolution operator (::) is used to differentiate them by namespace. The class namespace is the class name itself. The external function uses global scope unless scoped to another namespace. When the scope is not explicitly stated, then the scope is implied by the call site. Note that whenever there is any ambiguity about which function is implied, the compiler will emit an error indicating where the ambiguity lies, and the program will ultimately fail to compile.
Function is a logically grouped piece of code allowing perform certain types of operations. Function can return value (int, double etc) or be void type - meaning they do not return value. Functions allow to make code cleaner by moving out chunks of code of main body. For instance you want to write a function that finds area of rectangular. #include <iostream> using std cin; using std cout; using std endl; using std get; int main() { double sideA = 0.0; cout << "Enter side a of rectagular: "; cin >> sideA; double sideB = 0.0; cout << endl << "Enter side b of rectangular: "; cin >> sideB; //This part passes sides a and b to user defined function "Square" cout << endl << "Area of rectangular is: " << Square(a, b); cin.get(); return 0; } //definition of user defined function Square double Square(double a, double b) { return (a * b); }
You can't. If you have no main function, then there is no entry point to your code and it cannot be executed. Code without a main function is essentially a library. In MS Windows, GUI mode, you don't have to have main function. (WinMain is used instead).
In c++, variables exist in blocks of code and are destroyed after the block ends. Consider this example: <pre> #include <iostream> int main() { using namespace std; int x; cin << x; cout >> "you entered" >> x; return 0; } </pre> The variable x is declared in main, and is destroyed after main is finished. This means that x has <b>function scope</b>. It cannot be used outside main(). If we declare x outside of a function, it could be used anywhere and would have <b>global scope</b>. Learncpp.com has a good explanation of variable scope.
the code for the box is: # include <iostream> using namespace std; void main () { cout << "*********\n"; cout << "*\t*\n"; cout << "*\t*\n"; cout << "*\t*\n"; cout << "*\t*\n"; cout << "*\t*\n"; cout << "*\t*\n"; cout << "*\t*\n"; cout << "*********\n"; the code for the oval is: # include <iostream> using namespace std; void main () { cout << " ***\n"; cout <<" * *\n"; cout <<"* *\n"; cout <<"* *\n"; cout <<"* *\n"; cout <<"* *\n"; cout <<"* *\n"; cout <<" * *\n"; cout << " ***\n"; the code for the arrow is: # include <iostream> using namespace std; void main () { cout << " * \n"; cout <<" ***\n"; cout <<"*****\n"; cout <<" * \n"; cout <<" * \n"; cout <<" * \n"; cout <<" * \n"; cout <<" * \n"; the code for the diamond: # include <iostream> using namespace std; void main () { cout << " *\n "; cout <<" * *\n"; cout<<" * *\n"; cout<<" * *\n"; cout<<"* *\n"; cout<<" * *\n"; cout<<" * *\n"; cout <<" * *\n"; cout << " *\n "; }
#include #include void main() { long 11; clrscr(); cout<<"size of long="< getch(); }
In C++: #include <iostream> using namespace std; int main() { int x, y; char func; cout << "Enter a number: "; cin >> x; cout << "Choose Function: "; cin >> func; cout << "Enter another number: "; cin >> y; switch(func) { case '+': cout << x + y << endl; break; case '-': cout << x - y << endl; break; case '*': cout << x * y << endl; break; case '/': cout << x / y << endl; break; default: cout << "Invalid Function!" << endl; break; } char wait; cin >> wait; return 0; }
#include<iostream.h> void main() { cout<<' '<<' '<<"a"<<'\n'; cout<<' '<<"a"<<"b"<<"a"<<'\n'; cout<<'a'<<'b'<<'c'<<'b'<<'a'<<"\n"; }
You declare it outside of any class or function. Example: #include <iostream> using namespace std; int globalint = 52; int main(){ cout<<globalint<<endl; globalint=73; cout<<globalint<<endl; return(0) } Or, If you like C code: #include <stdio.h> int globalint = 52; int main(){ printf ("GlobalInt: %d\n", globalint); globalint=73; printf ("GlobalInt: %d\n", globalint); exit(0); }
You declare it outside of any class or function. Example: #include <iostream> using namespace std; int globalint = 52; int main(){ cout<<globalint<<endl; globalint=73; cout<<globalint<<endl; return(0) } Or, If you like C code: #include <stdio.h> int globalint = 52; int main(){ printf ("GlobalInt: %d\n", globalint); globalint=73; printf ("GlobalInt: %d\n", globalint); exit(0); }
Yes you can use the same function name for a member function and an external function. They are primarily distinguished by the number and type of arguments they accept (the function signature). If they match exactly, then the scope resolution operator (::) is used to differentiate them by namespace. The class namespace is the class name itself. The external function uses global scope unless scoped to another namespace. When the scope is not explicitly stated, then the scope is implied by the call site. Note that whenever there is any ambiguity about which function is implied, the compiler will emit an error indicating where the ambiguity lies, and the program will ultimately fail to compile.
Function is a logically grouped piece of code allowing perform certain types of operations. Function can return value (int, double etc) or be void type - meaning they do not return value. Functions allow to make code cleaner by moving out chunks of code of main body. For instance you want to write a function that finds area of rectangular. #include <iostream> using std cin; using std cout; using std endl; using std get; int main() { double sideA = 0.0; cout << "Enter side a of rectagular: "; cin >> sideA; double sideB = 0.0; cout << endl << "Enter side b of rectangular: "; cin >> sideB; //This part passes sides a and b to user defined function "Square" cout << endl << "Area of rectangular is: " << Square(a, b); cin.get(); return 0; } //definition of user defined function Square double Square(double a, double b) { return (a * b); }
You can't. If you have no main function, then there is no entry point to your code and it cannot be executed. Code without a main function is essentially a library. In MS Windows, GUI mode, you don't have to have main function. (WinMain is used instead).
Function to print star triangle in c++#include#includevoid starfunction(int); //You can give any function namevoid main(){int a;clrscr();couta;star(a);getch();}void starfunction(int r ){for(int i=1;i
#include#includeusing std::cin;using std::cout;using std::endl;using std::string;int main(){string number = "0";cout number;cout