object oriented programming lecture 1

13
CSC 103 - Object Oriented Programming Spring, 2011 Lecture 1, Background 14 th Feb, 2011 Instructor: M. Anwar-ul-Haq

Upload: anwar-ul-haq

Post on 22-Dec-2014

1.443 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Object Oriented Programming lecture 1

CSC 103 - Object Oriented Programming

Spring, 2011

Lecture 1, Background

14th Feb, 2011

Instructor: M. Anwar-ul-Haq

Page 2: Object Oriented Programming lecture 1

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad

2

First Program

// my first program in C++

#include <iostream> using namespace std;

int main () {

cout << “Hello World"; return 0;

}

Page 3: Object Oriented Programming lecture 1

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad

3

Background

• //  comments • Lines beginning with a hash sign (#) are

directives for the preprocessor. • #include <iostream> tells the preprocessor

to include the iostream standard file. • using namespace std;  Elements of the

standard C++ library are declared within what is called a namespace, the namespace with the name std.

Page 4: Object Oriented Programming lecture 1

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad

4

Background

• int main () The main function is the point by where all C++ programs start their execution, independently of its location within the source code. 

• For that same reason, it is essential that all C++ programs have a main function.

Page 5: Object Oriented Programming lecture 1

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad

5

Background

• cout << "Hello World!";

• cout is the name of the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World sequence of characters) into the standard output stream (cout, which usually corresponds to the screen).

Page 6: Object Oriented Programming lecture 1

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad

6

Background

• cout is declared in the iostream standard file within the std namespace, so that's why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code.

Page 7: Object Oriented Programming lecture 1

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad

7

Background

• This operator >> applied to an input stream (cin) is known as extraction operator. 

• The << operator applied to an output stream (cout) is known as insertion operator. 

Page 8: Object Oriented Programming lecture 1

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad

8

C++ Structures

• A collection of one or more variables, typically of different types, grouped together under a single name for convenient handling

• Known as struct in C and C++

Page 9: Object Oriented Programming lecture 1

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad

9

C++ Structures• Structures

– Aggregate data types built using elements of other types struct Time{

int hour;

int minute;

int second;

}; – Members of the same structure must have unique names– Two different structures may contain members of the

same name– Each structure definition must end with a semicolon

Structure tag

Structure members

Page 10: Object Oriented Programming lecture 1

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad

10

C++ Structures

• struct – Creates a new data type that is used to

declare variables– Structure variables are declared like variables

of other types

Page 11: Object Oriented Programming lecture 1

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad

11

C++ Structures

• Member access operators:– Dot operator (.) for structures and objects– Arrow operator (->) for pointers– Print member hour of timeObject:

cout << timeObject.hour;

Page 12: Object Oriented Programming lecture 1

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad

12

Examples

struct point { int x; int y; };

struct point p1, p2;

p1 and p2 are bothpoints, containing anx and a y value

struct { int x; int y;} p1, p2;

p1 and p2 bothhave the definedstructure, containingan x and a y, butdo not have a tag

struct point { int x; int y;} p1, p2;

same as the othertwo versions, butunited into one setof code, p1 and p2have the tag point

For the first and last sets of code, point is a defined tag and can be usedlater (to define more points, or to declare a type of parameter, etc) but inthe middle code, there is no tag, so there is no way to reference moreexamples of this structure

Page 13: Object Oriented Programming lecture 1

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad

13

Example#include <iostream>#include <string>//using std::string;using namespace std;

struct employee{

int id_number;int age;string name;float salary;

};

int main(){ employee emp;

emp.id_number=1; emp.age = 25; emp.name="adsdfsd"; emp.salary =60000; cout<<"Employee id:"<<emp.id_number<<endl; cout<<"Employee Age:"<<emp.age<<endl; cout<<"Employee Name:"<<emp.name<<endl; cout<<"Employee Salary:"<<emp.salary<<endl; return 0;}