0% found this document useful (0 votes)
104 views

Assignment 0 OOP

This document contains the details of an assignment for an Object Oriented Programming course. It lists 22 tasks related to C++ programming concepts like variables, data types, functions, pointers, structures, and file handling. The student is required to submit solutions to the listed tasks to their lecturer by March 17, 2021.

Uploaded by

HUZAIFA SAEED
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Assignment 0 OOP

This document contains the details of an assignment for an Object Oriented Programming course. It lists 22 tasks related to C++ programming concepts like variables, data types, functions, pointers, structures, and file handling. The student is required to submit solutions to the listed tasks to their lecturer by March 17, 2021.

Uploaded by

HUZAIFA SAEED
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Course Tittle:

Object Oriented Programming

Course Code:

CC 1022

Assignment Topic : “Assignment 0”


Due Date for Submission : “Wednesday, 17-03-2021”
Submitted to : “Mehr Un Nisa”
Lecturer
School of Systems And Technology (SST)

Name : Huzaifa Saeed


Student Id : F2020266522
Section : V2
BSCS (Computer Sciences)
Task 01: Scope of variables

Global variable example: Local variable example:

• int count; // global • void main()


variable • {
• void main() • int count = 0; // local
• { variable
• count = count + 1; • count = count + 1;
• } }

Task 02: Else-if ladder

#include <iostream>
void main()
{
int x = -1;

if ( x > 0 ) {

std::cout << "x is positive";

} else if ( x < 0 ) {

std::cout << "x is negative";

} else {

std::cout << "x is zero";


}
}

Task 03: Break Statement

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;
• }
}

Task 04: Continue Statement

Break cannot be used to exit if-else code blocks:

• 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...
}

Task 05: Arrays

• char carr[ ] = { 'a', 'b', 'c', 0 }; // ok: size is


calculated

• int iarr[ 2 ] = { 5 }; // ok: same as { 5, 0 }

• float fa[ 3 ] = { x, y, z }; // variable initializers
ok

• int ibad[ 2 ] = { 1, 2, 3 }; // error: too many
initializers

• ibad[ 2 ] = { 10, 20 }; // error: no initializer-
assignments

Task 06: Array as Function Argument


• #include <iostream>
• using namespace std;
• void foo( double dbls[] )
• {
• cout
• << "in foo() size of dbls is "
• << sizeof( dbls )
• << '\n'
• ;
• }
• int main()
• {
• double dummy[ 4 ] = { 0 };
• cout
• << "in main() size of dummy is "
• << sizeof(dummy)
• << '\n'
• ;
• foo( dummy );
• return 0;
• }

Task 07: Two Dimensional Array

• 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

The ampersand operator & returns address of the variable:



• #include <iostream>
• using namespace std;
• int main( )
• {
• int x = 5;
• double d = 0.1;

• // Taking variable addresses:
• cout << &x << '\n';
• cout << &d << '\n';

• return 0;
• }

Task 09: Address 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;
• }

Task 10: Storing address of a variable

• 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;
• }

Task 11: Pointer to a C++ variable

• 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;
• }

Task 12: Pointer dereference *ptr

provides access to the variable pointed by the address:

x = *ptr; // fetch the value at the address specified by ptr


*ptr = 5; // store new value at the address specified by ptr
#include <iostream>
using namespace std;
void print( char* message )
{
cout << message << '\n';
}

Task 13: Pointer as function argument

int main( )
{
char* phello = "Hello";
print( phello );
print( "World" );
return 0;
};

Task 14: Passing Arrays to Functions

#include <iostream>
using namespace std;

// declare function to display marks


// take a 1d array as parameter
void display(int m[5]) {
cout << "Displaying marks: " << endl;

// display array elements


for (int i = 0; i < 5; ++i) {
cout << "Student " << i + 1 << ": " << m[i] << endl;
}
}
int main() {

// declare and initialize an array


int marks[5] = {88, 76, 90, 61, 69};

// call display function


// pass array as argument
display(marks);

return 0;
}

Task 15: Strings

#include <iostream>
using namespace std;

int main()
{
char str[100];

cout << "Enter a string: ";


cin >> str;
cout << "You entered: " << str << endl;

cout << "\nEnter another string: ";


cin >> str;
cout << "You entered: "<<str<<endl;

return 0;
}

Task 16: Arrays of pointers


char* ptr = "hello"; // pointer
char arr[] = "hello"; // array
void swap( int* px, int* py )
{
int temp = *px;
*px = *py;
*py = temp;
}

Task 19: Implementing swap( ) function

int main( )
{
int x = 5;
int y = 10;
swap( x, y ); // Error: must take address
swap( &x, &y ); // Correct
return 0;
}

Task 20: Structures

struct Person
{
char name[50];
int age;
float salary;
};

Task 21: Passing Structure to functions

#include <iostream>
using namespace std;
struct Person {
char name[50];
int age;
float salary;
};

void displayData(Person); // Function declaration

int main() {
Person p;

cout << "Enter Full name: ";


cin.get(p.name, 50);
cout << "Enter age: ";
cin >> p.age;
cout << "Enter salary: ";
cin >> p.salary;

// Function call with structure variable as an argument


displayData(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;
}

Task 22: Sequential File Handling

#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;
}

You might also like