Assignment 0 OOP
Assignment 0 OOP
Course Code:
CC 1022
#include <iostream>
void main()
{
int x = -1;
if ( x > 0 ) {
} else if ( x < 0 ) {
} else {
Break causes an immediate exit from the switch. Break also causes exit
from while and for:
• void main()
• {
• int x = 0;
• while ( x < 5 ) { // condition
• // loop body:
• x = x + 1;
• if ( x == 3 )
• break;
• }
}
• void main()
• {
• int x = 0;
• if ( x < 5 ) {
• break; // Error: illegal break
• }
}
for ( i = 0; i < n; ++i ) {
int result = get_data( i );
if ( result < 0 ) // skip negative values
continue;
// process zero and positive values...
}
• int main()
• {
• for (;;) {
• int month;
• int day;
• int year;
•
• std::cout << "Enter M D Y: ";
• std::cin >> month;
• std::cin >> day;
• std::cin >> year;
•
• std::cout
• << "day of year: "
• << day_of_year( month, day, year )
• << std::endl
• ;
• }
•
• return 0;
• }
Task 08: Properties of a C++ variable
• int main( )
• {
• // Local variables:
• int x = 5;
• double d = 0.1;
•
• // Variable sizes:
• int x_bytes = sizeof( x );
• int d_bytes = sizeof( d );
•
• // What about variable addresses ?
• int x_addr = addressof( x );
• int d_addr = addressof( d );
•
• return 0;
• }
• int main( )
• {
• int x = 5;
• double d = 0.1;
•
• // Variable sizes:
• int x_bytes = sizeof( x );
• int d_bytes = sizeof( d );
•
• // Take and store variable addresses:
• int x_addr = &x;
• int d_addr = &d;
•
• return 0;
• }
• int main( )
• {
• int x = 5;
• double d = 0.1;
•
• // Variable sizes:
• int x_bytes = sizeof( x );
• int d_bytes = sizeof( d );
•
• // We need pointers to store addresses:
• int* x_ptr = &x;
• double* d_ptr = &d;
•
• return 0;
• }
int main( )
{
char* phello = "Hello";
print( phello );
print( "World" );
return 0;
};
#include <iostream>
using namespace std;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
char str[100];
return 0;
}
int main( )
{
int x = 5;
int y = 10;
swap( x, y ); // Error: must take address
swap( &x, &y ); // Correct
return 0;
}
struct Person
{
char name[50];
int age;
float salary;
};
#include <iostream>
using namespace std;
struct Person {
char name[50];
int age;
float salary;
};
int main() {
Person p;
return 0;
}
void displayData(Person p) {
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p.name << endl;
cout <<"Age: " << p.age << endl;
cout << "Salary: " << p.salary;
}
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream output_file;
output_file.open( "filename.txt" );
output_file << "Hello";
output_file.close();
return 0;
}