160 likes | 260 Views
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.
E N D
CSI 218Object Oriented Programming SessionalLab 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. • Functions transform data from one form to another. • Employes top-down approach in program design.
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.
Basic Concepts • Objects • Class
Basic Concepts (Cont.) • Benefits: • Data abstraction and encapsulation • Inheritance • Polymorphism • Dynamic binding • Message passing
objects • Objects are the basic run-time entities in an object-oriented system • They may represent, a place, a bank account, a table
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.
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.
Sample Program #include <iostream> using namespace std; int main(){ cout<<“C++ is better that C.\n”; return 0; }
The iostream file • This directive causes the preprocessor to add the contents of the iostream file to the program. It contains the identifier coutthe operator <<. • Some old versions of c++ use a header file called iostream.h
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.
The operator “<<” is called the insertion or put to operator. • The operator “>>” is calledextraction 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.
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; }
#include <iostream> using namespace std; // Class Declaration class person { //Access - Specifier public: string name; int number; }; //Main Function int 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; }
#include <iostream> using namespace std; class Box { public: int width; void printWidth( Box box ); void setWidth( intwid ); }; // Member function definition void Box::setWidth( intwid ) { 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; }
// Main function for the program int main( ) { Box box; int width; cout<<"Enter width: "; cin>>width; // set box width without member function box.setWidth(width); box.printWidth(box); return 0; }