csi 218 object oriented programming sessional lab 1

16
CSI 218 Object Oriented Programming Sessional Lab 1 Presented By: Nazia Hossain

Upload: maj

Post on 05-Jan-2016

13 views

Category:

Documents


0 download

DESCRIPTION

CSI 218 Object Oriented Programming Sessional Lab 1. Presented By: Nazia Hossain. In POP. Emphasis on steps/algorithm. Large programs are divided into smaller programs known as functions. Most of the functions share global data. Data move openly around the system from function to function. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSI 218 Object Oriented Programming  Sessional Lab 1

CSI 218Object Oriented Programming Sessional

Lab 1

Presented By:Nazia Hossain

Page 2: CSI 218 Object Oriented Programming  Sessional Lab 1

In POP

• Emphasis on steps/algorithm.• Large programs are divided into smaller programs

known as functions.• Most of the functions share global data.• Data move openly around the system from

function to function.• Functions transform data from one form to

another.• Employes top-down approach in program design.

Page 3: CSI 218 Object Oriented Programming  Sessional Lab 1

In OOP

• Emphasis is on data rather than procedure• Programs are divided into what are known as objects• Data structures are designed such that they characterize the

objects.• Functions that operate on the data of an object are tied

together in the data structure.• Data is hidden and cannot be accessed by external functions• Objects may communicate with each other through function• New data and functions can be easily added whenever

necessary• Follows bottom-up approach in program design.

Page 4: CSI 218 Object Oriented Programming  Sessional Lab 1

Basic Concepts

• Objects• Class

Page 5: CSI 218 Object Oriented Programming  Sessional Lab 1

Basic Concepts (Cont.)

• Benefits:– Data abstraction and encapsulation– Inheritance– Polymorphism– Dynamic binding– Message passing

Page 6: CSI 218 Object Oriented Programming  Sessional Lab 1

objects

• Objects are the basic run-time entities in an object-oriented system

• They may represent, a place, a bank account, a table

Page 7: CSI 218 Object Oriented Programming  Sessional Lab 1

Class

• The entire set of data and code of an object can be made a user-defined data type with the help of a class.

• Objects are variables of the type class• Once a class has been defined, we can create any

number of objects belonging to that class• A class is thus a collection of objects of similar type.• For example: mango, apple and orange are

members of the class fruit.

Page 8: CSI 218 Object Oriented Programming  Sessional Lab 1

Structure and Class

• If you define a structure and then declare an object of that structure, the members of the object are public by default.

• But for class in c++, the members of the class is private by default.

Page 9: CSI 218 Object Oriented Programming  Sessional Lab 1

Sample Program

#include <iostream>using namespace std;

int main(){cout<<“C++ is better that C.\n”;return 0;}

Page 10: CSI 218 Object Oriented Programming  Sessional Lab 1

• The iostream file– This directive causes the preprocessor to add the

contents of the iostream file to the program. It contains the identifier cout the operator <<.

– Some old versions of c++ use a header file called iostream.h

Page 11: CSI 218 Object Oriented Programming  Sessional Lab 1

• Namespace:– A new concept introduced by the ANSI C++– This defines a scope for the identifiers that are used

in a program– For using the identifiers defined in the namespace

scope we must include the using directive, like using namespace std;– std is the namespace where ANSI C++ standard

class libraries are defined. And brings all the identifiers in global scope.

Page 12: CSI 218 Object Oriented Programming  Sessional Lab 1

• The operator “<<” is called the insertion or put to operator.

• The operator “>>” is called extraction or get from operator.

• Casecading I/O operator:– We can use insertion operator << repeatedly for

printing results.– Multiple use of << in one statement is called

casecading.

Page 13: CSI 218 Object Oriented Programming  Sessional Lab 1

More Sample Program#include <iostream>using namespace std;

int main(){float number1, number2, sum, average;cout<<“enter two numbers:”;cin>>number1;cin>>number2;

sum=number1+number2;average=sum/2;

cout<<“sum: ”<<sum<<“\n”;cout<<“average: ”<<average<<“\n”;return 0;}

Page 14: CSI 218 Object Oriented Programming  Sessional Lab 1

#include <iostream>using namespace std;

// Class Declarationclass person{//Access - Specifierpublic: string name; int number;};

//Main Functionint main(){ person obj; cout<<"Enter the Name :"; cin>>obj.name; cout<<"Enter the Number :"; cin>>obj.number; cout << obj.name << ": " << obj.number << endl; return 0;}

Page 15: CSI 218 Object Oriented Programming  Sessional Lab 1

#include <iostream>using namespace std;

class Box{public: int width; void printWidth( Box box ); void setWidth( int wid );};

// Member function definitionvoid Box::setWidth( int wid ){ width = wid;}void Box::printWidth( Box box ){ /* Because setWidth() is a friend of Box, it can directly access any member of this class */ cout << "Width of box : " << box.width <<endl;}

Page 16: CSI 218 Object Oriented Programming  Sessional Lab 1

// Main function for the programint main( ){ Box box; int width; cout<<"Enter width: "; cin>>width; // set box width without member function box.setWidth(width); box.printWidth(box); return 0;}