[ C++ ] DAY-2 (call function, sizeof, typedef, unsigned_loop)

Posted: December 31, 2010 in C++

#include <iostream>
void DemonstrationFunction();  //if other functions’ coding after main function, should have prototype 

int main()
{
 std::cout << “In main\n”;
 DemonstrationFunction();
 std::cout << “Back in main\n”;
 return 0;
}

void DemonstrationFunction()
{
 std::cout << “In Demonstration Function\n”;
}

 

#include <iostream>

void DemonstrationFunction() //If other functions in front of main function, no need have function prototype
{
 std::cout << “In Demonstration Function\n”;
}

int main()
{
 std::cout << “In main\n”;
 DemonstrationFunction();
 std::cout << “Back in main\n”;
 return 0;
}

The same output:

#include <iostream>

int Add(int first, int second)
{
 std::cout << “Add(), received ” << first << ” and ” << second << “.\n”;
 return (first + second);
}

int Substract (int first, int second)
{
 std::cout << “Substract(), received ” << first << ” and ” << second << “.\n”;
 return (first – second);
}

int main()
{
 using std::cout;
 using std::cin;
 
 int a,b,c;
 cout<< “Enter 2 numbers: \n”;
 cin >> a;
 cin >> b;

 cout << “\nCalling Add():\n”;
 c=Add(a,b);
 cout << “\nBack in main().\n”;
 cout << “c was set to ” << c << ” .\n”;
 cout << “\nCalling Substract():\n”;
 c=Substract(a,b);
 cout << “\nBack in main().\n”;
 cout << “c was set to ” << c << ” .\n”;

 cout << “\nExiting…\n\n”;
 return 0;
}

#include <iostream>

int main()
{
 using std::cout;

//”sizeof”  will help define the length of the type which are excuting of the program
 cout << “The size of an int is:\t\t” << sizeof (int) << ” bytes.\n”;
 cout << “The size of a short is:\t\t” << sizeof (short) << ” bytes.\n”;
 cout << “The size of a long is:\t\t” << sizeof (long) << ” bytes.\n”;
 cout << “The size of a char is:\t\t” << sizeof (char) << ” bytes.\n”;
 cout << “The size of a float is:\t\t” << sizeof (float) << ” bytes.\n”;
 cout << “The size of a double is:\t” << sizeof (double) << ” bytes.\n”;
 cout << “The size of a bool is:\t\t” << sizeof (bool) << ” bytes.\n”;
}

//Demostrates typedef keyword
#include <iostream>

typedef unsigned short int Ethan; //define “Ethan” as “unsigned short int”

int main()
{
 using std::cout;
 using std::endl;

 //using “Ethan”
 Ethan Width = 5;
 Ethan Length = 10;
 Ethan Area = Width * Length;
 cout << “Width: ” << Width << endl;
 cout << “Length: ” << Length << endl;
 cout << “Area: ” << Area << endl;
 return 0;
}

/*
As we all know the unsigned type has its range, but if it expands the range, then what will happen ?
Here is an example to show you what will happen.

When expanding the range, it will like the car’s  odometer, will loop from the begin of the length.
Here, the maximum of unsigned short int is 65535 (range 0-65535), then over the size, it will start from 0.

We define it as 65535, then just like C++ for C, we let maxnumber “++” (maxnumber++),
see what will happen !!
*/

#include <iostream>

int main()
{
 using std::cout;
 using std::endl;

 unsigned short int maxnumber;
 maxnumber = 65535; //the maximum of unsigned short int is 65535 (range 0-65535)
 cout << “Maximum number: ” << maxnumber << endl;
 maxnumber++;
 cout << “Maximum number: ” << maxnumber << endl;
 maxnumber++;
 cout << “Maximum number: ” << maxnumber << endl;
 return 0;
}

Hope you understand !

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s