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
”.
Function Prototype
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( 900.0 );
 
sqrt (square root) function
 The preceding statement would print 30
 All functions in math library return a 
double
Function 
Prototype
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
Function 
Calling
cout<< sqrt(9);
            3
O
u
t
p
u
t
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)
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
Function 
Definition
     
// 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
    
    
// 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                                   
1  4  9  16  25  36  49  64  81  100
Function 
Example
 
Classes And Objects
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
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).
Classes And Objects
class 
class_name
{
 
….
 
….
 
….
};
Class body  (data
member + 
methods
methods
)
Any valid identifier
Class Specification
Syntax:
   class 
class_name
 
{
 
};
Class Specification
class
 Student
    
{
       int st_id;
 
   char st_name[10];
 
   void read_data();
 
   void print_data();
   
};
 
D
a
t
a
 
M
e
m
b
e
r
s
 
o
r
 
P
r
o
p
e
r
t
i
e
s
 
o
f
S
t
u
d
e
n
t
 
C
l
a
s
s
 
M
e
m
b
e
r
s
 
F
u
n
c
t
i
o
n
s
 
o
r
B
e
h
a
v
i
o
u
r
s
 
o
f
 
 
S
t
u
d
e
n
t
 
C
l
a
s
s
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.
Classes And Objects
class 
class_name
{
     private:
 
 
 
     public:
 
 
 
};
 
Class Access Example
class
 Student
    
{
    private :   int st_id;
 
   
  
  char st_name[10];
 
public  :   void read_data();
 
  
  
 void print_data();
   
};
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.
Classes
This class example shows how we can
encapsulate (gather) a circle information into
one package (unit or class)
Classes
class Circle
{
     private:
 
double radius;
     public:
 
void setRadius(double r);
 
double getDiameter();
 
double getArea();
 
double getCircumference();
};
Class implementation: writing the code of class
methods.
There are two ways:
1.
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
( ){
}
Method Implementation
2.
Member functions defined inside class
Do not need scope resolution operator, class
name;
Method Implementation
class Circle
{
     private:
 
double radius;
     public:
 
void setRadius(double r){radius = r;}
 
double getDiameter(){ return radius *2;}
 
double getArea();
 
double getCircumference();
};
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
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);
}
void main()
{
    Circle c(7);
    Circle *cp1 = &c;
    Circle *cp2 = new Circle(7);
    
    cout<<“The are of cp2:”
  
<<cp2->getArea();
    
}
Method Implementation
Operators to access class members
Identical to those for 
struct
s
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
Accessing Class Member
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;
Objects
ASSIGNMENT
Design a class Student With Its
     RollNo, Fees : Data member
     setRollNo , displayStudentdata  : Member
                                                            functions
Create two different Student objects and set
their RollNo and Fees and display Their Data()
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( );
}
ASSIGNMENT
Design a class Circle With Its
     radius: Data member
     setRadius , getArea, getCircumference,
getDiameter  : Member  functions
Create two different Circle objects and set their
Radius display Area, Diameter and
Circumference of Circle
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;
}
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()
}
Slide Note
Embed
Share

Large programs are best developed and maintained by dividing them into smaller modules through the technique called "Divide and Conquer." This approach enhances code organization, reusability, and maintainability. Learn about function prototypes, definitions, calling, and examples to effectively implement modular programming in your projects.

  • Modular programming
  • Function prototype
  • Divide and Conquer
  • Code organization
  • Maintainability

Uploaded on Feb 22, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. 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 Bad Development Approach main() { ----- } main() { ----- ----- ----- . . ---- ----- ----- Return 0; } function f1() { --- } function f2() { --- }

  2. Function Prototype 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( 900.0 ); sqrt (square root) function The preceding statement would print 30 All functions in math library return a double

  3. 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. 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. 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 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. 1 4 9 16 25 36 49 64 81 100

  7. Classes And Objects

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

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

  10. 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. 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 Syntax: class class_name { Data members Members functions };

  13. 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 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. Classes And Objects class class_name { private: public: };

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

  17. 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. 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. class Circle { private: public: }; double radius; They are accessible from outside the class, and they can access the member (radius) void setRadius(double r); double getDiameter(); double getArea(); double getCircumference();

  19. Method Implementation Class implementation: writing the code of class methods. There are two ways: 1. 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 2. Member functions defined inside class Do not need scope resolution operator, class name; class Circle { private: public: }; Defined inside class double radius; void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference();

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

  22. Method Implementation The second constructor is called Since radius is a private class data member 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 ; }

  23. class Circle { private: public: }; Circle::Circle(int r) { radius = r; } double Circle::getArea() { return radius * radius * (22.0/7); } double Circle:: getCircumference() { return 2 * radius * (22.0/7); } double radius; Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); 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 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. 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 Design a class Student With Its RollNo, Fees : Data member setRollNo , displayStudentdata : Member functions Create two different Student objects and set their RollNo and Fees and display Their Data()

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

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

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

  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() }

Related


More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#