Presentation is loading. Please wait.

Presentation is loading. Please wait.

This technique is Called “Divide and Conquer”.

Similar presentations


Presentation on theme: "This technique is Called “Divide and Conquer”."— Presentation transcript:

1 This technique is Called “Divide and Conquer”.
OOP Function Prototype Experience has shown that the best way to develop and maintain large programs is to construct it from smaller pieces(Modules) This technique is Called “Divide and Conquer”. Wise Development Approach main() { ----- } function f1() --- function f2() main() { ----- . ---- Return 0; } Bad Development Approach

2 Function Call : Example or OOP Function Prototype int square( int );
Tells compiler argument type and return type of function int square( int ); Function takes an int and returns an int Function Call : functionName (argument); or functionName(argument1, argument2, …); Example cout << sqrt( ); sqrt (square root) function The preceding statement would print 30 All functions in math library return a double

3 Calling/invoking a function sqrt(x);
OOP Function Calling Calling/invoking a function sqrt(x); Parentheses an operator used to call function Pass argument x Function gets its own copy of arguments After finished, passes back result Output argument Function Name 3 cout<< sqrt(9); Parentheses used to enclose argument(s)

4 Declarations of local variables and Statements
OOP Function Definition Syntax format for function definition returned-value-type function-name (parameter-list) { Declarations of local variables and Statements } Parameter list Comma separated list of arguments Data type needed for each argument If no arguments, use void or leave blank Return-value-type Data type of result returned (use void if nothing returned)

5 Functions cannot be defined inside other functions
OOP Function Definition Example function int square( int y ) { return y * y; } return keyword Returns data, and control goes to function’s caller If no data to return, use return; Function ends when reaches right brace Control goes to caller Functions cannot be defined inside other functions

6 // Creating and using a programmer-defined function.
#include <iostream.h> int square( int ); // function prototype int main() { // loop 10 times and calculate and output // square of x each time for ( int x = 1; x <= 10; x++ ) cout << square( x ) << " "; // function call cout << endl; return 0; // indicates successful termination } // end main OOP Function prototype: specifies data types of arguments and return values. square expects an int, and returns an int. Parentheses () cause function to be called. When done, it returns the result. Function Example // square function definition returns square of an integer int square( int y ) // y is a copy of argument to function { return y * y; // returns square of y as an int } // end function square Definition of square. y is a copy of the argument passed. Returns y * y, or y squared.

7 OOP Classes And Objects

8 Method Implementation in C++
OOP Classes And Objects Defining A Class Data Members Access specifier Constructors Method Implementation in C++ Accessing Class Member Destructors

9 Static Data Members and Functions
OOP Classes And Objects Static Data Members and Functions Array Of Objects Class as ADTs

10 Classes are user-defined (programmer-defined) types.
OOP Classes And Objects Classes are user-defined (programmer-defined) types. Data (data members) Functions (member functions or methods) In other words, they are structures + functions

11 A class definition begins with the keyword class.
OOP Classes And Objects A class definition begins with the keyword class. The body of the class is contained within a set of braces, { } ; (notice the semi-colon). class class_name { …. }; Any valid identifier Class body (data member + methods)

12 Class Specification Data members Members functions Syntax:
OOP Class Specification Syntax: class class_name { }; Data members Members functions

13 Class Specification class Student { int st_id; char st_name[10];
OOMP Class Specification class Student { int st_id; char st_name[10]; void read_data(); void print_data(); }; Data Members or Properties of Student Class Members Functions or Behaviours of Student Class

14 Class Specification Visibility of Data members & Member functions
OOP Class Specification Visibility of Data members & Member functions public - accessed by member functions and all other non-member functions in the program. private - accessed by only member functions of the class. protected - similar to private, but accessed by all the member functions of immediate derived class default - all items defined in the class are private.

15 OOP Classes And Objects class class_name { private: public: };

16 Class Access Example class Student { private : int st_id;
OOP Class Access Example class Student { private : int st_id; char st_name[10]; public : void read_data(); void print_data(); };

17 OOP Classes Within the body, the keywords private: and public: specify the access level of the members of the class. the default is private. Usually, the data members of a class are declared in the private: section of the class and the member functions are in public: section.

18 OOP Classes This class example shows how we can encapsulate (gather) a circle information into one package (unit or class) No need for others classes to access and retrieve its value directly. The class methods are responsible for that only. They are accessible from outside the class, and they can access the member (radius) class Circle { private: double radius; public: void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); };

19 Method Implementation
OOP Method Implementation Class implementation: writing the code of class methods. There are two ways: Member functions defined outside class Using Binary scope resolution operator (::) “Ties” member name to class name Uniquely identify functions of particular class Different classes can have member functions with same name Format for defining member functions ReturnType ClassName::MemberFunctionName( ){ }

20 Method Implementation
OOP Method Implementation Member functions defined inside class Do not need scope resolution operator, class name; class Circle { private: double radius; public: void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; Defined inside class

21 Method Implementation
OOP Method Implementation class Circle { private: double radius; public: void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; double Circle::getArea() return radius * radius * (22.0/7); } double Circle:: getCircumference() return 2 * radius * (22.0/7); Defined outside class

22 Method Implementation
OOP Method Implementation void main() { Circle c1,c2(7); cout<<“The area of c1:” <<c1.getArea()<<“\n”; //c1.raduis = 5;//syntax error c1.setRadius(5); cout<<“The circumference of c1:” << c1.getCircumference()<<“\n”; cout<<“The Diameter of c2:” <<c2.getDiameter()<<“\n”; } The second constructor is called Since radius is a private class data member

23 Method Implementation
class Circle { private: double radius; public: Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; Circle::Circle(int r) radius = r; } double Circle::getArea() return radius * radius * (22.0/7); double Circle:: getCircumference() return 2 * radius * (22.0/7); OOPCGL void main() { Circle c(7); Circle *cp1 = &c; Circle *cp2 = new Circle(7); cout<<“The are of cp2:” <<cp2->getArea(); } Method Implementation

24 Accessing Class Member
OOP Accessing Class Member Operators to access class members Identical to those for structs Dot member selection operator (.) Object Reference to object Arrow member selection operator (->) Pointers Accessing a Class Member through these operators requires an object of the class

25 You can instantiate many objects from a class type.
OOP Objects Declaring a variable of a class type creates an object. You can have many variables of the same type (class). Instantiation Once an object of a certain class is instantiated, a new memory location is created for it to store its data members and code You can instantiate many objects from a class type. Ex) Circle c; Circle *c;

26 ASSIGNMENT OOP Design a class Student With Its RollNo, Fees : Data member setRollNo , displayStudentdata : Member functions Create two different Circle objects and set their Roll Nos and fees and display Their data

27 class Student { private: int nRollno; float fFees; public: void setRollNo( ){ cin>>nRollNo>>fFees; } void displayStudentData() { cout<<nRollNo<<fFees; } }; void main( ) Student s1,s2; s1.setRollNo( ); s1.displayStudentData( ); s2.setRollNo( ); s2.displayStudentData( ); }

28 ASSIGNMENT OOP Design a class Circle With Its radius: Data member setRadius , getArea, getCircumference, getDiameter : Member functions Create two different Student objects and set their Radius display Area, Diameter and Circumference of Circle

29 class Circle { private: double radius; public: void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; double Circle::getArea() return radius * radius * (22.0/7); } double Circle:: getCircumference() return 2 * radius * (22.0/7); double Circle:: getDiameter() return 2 * radius;

30 void main() { Circle c1,c2; c1.setRadius(5); c2.setRadius(7); cout<<“The area of c1:” <<c1.getArea()<<“\n”; cout<<“The area of c2:” <<c2.getArea()<<“\n”; //c1.raduis = 5;//syntax error cout<<“The circumference of c1:” << c1.getCircumference()<<“\n”; cout<<“The Diameter of c2:” <<c2.getDiameter() }


Download ppt "This technique is Called “Divide and Conquer”."

Similar presentations


Ads by Google