SlideShare a Scribd company logo
C++ Review:  FUNCTIONS
VOID FUNCTIONS #include <iostream.h> void addoreven(); int main() { addoreven(); cout << &quot;That's all!\n&quot;; return 0; } void addoreven() { int  your_number; cout << &quot;Enter a whole number: &quot;; cin >> your_number; if (your_number % 2 == 0)   cout << &quot;Your number is even\n&quot;; else {   cout << &quot;Your number is odd.\n&quot;; } }
FUNCTIONS that RETURN VALUES #include <iostream.h> #include <conio.h> // function square computes the square of x int square (int x) { return x * x; } // main is where program execution begins void main () { int i; for (i = 0;  i <= 20;  i = i + 1) { cout << &quot;\nvalue = &quot; << i << &quot; square = &quot; << square(i); } // wait for key pressure getch(); }
PROGRAMMING EXERCISE: Using Functions that return values, create a program that converts Farenheit values from 2 to 222 (incremented by 2) to Celsius  To convert into Celsius, the following conversion factor is used: Celsius = 5/9 * (Farenheit – 32)
#include <iostream.h> float celsius(float);  int main() { for (int x=2; x<232; x += 10) cout << x << &quot;\t&quot; << celsius(x) << &quot;\n&quot;; } float celsius(float x) { return 5.0/9.0*(x-32); }
PROGRAMMING EXERCISE: Using Functions that return values, create a program that displays the factorial of 0 to 10.  Factorial example: 5! = 5 * 4 * 3 * 2 * 1
PROGRAMMING EXERCISE: Using Functions that return values, create a program that reverses a given input string e.g. Rhej Cachero Output: orehcaC jehR
Class an expanded concept of a data structure: instead of holding only data, it can hold both data and functions.  a new kind of data type; each class that you define is a collection of data ( data members)  plus the ability to include special functions  (member functions).
Class An  object  is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.
Classes are generally declared using the keyword class, with the following format:  class class_name { access_specifier_1: member1; access_specifier_2: member2; ... } object_names; Where  class_name  is a valid identifier for the class,  object_names  is an optional list of names for objects of this class. The body of the declaration can contain members, that can be either data or function declarations, and optionally access specifiers.  Class
Class An access specifier  is one of the following three keywords:  private ,  public   or   protected . These specifiers modify the access rights that the members following them acquire:  private  members  of a class are accessible only from within other members of the same class  public  members  are accessible from anywhere where the object is visible.
Class By default,  all members of a class declared with the class keyword have private access for all its members . Therefore, any member that is declared before one other class specifier automatically has private access.
Class class CRectangle {  int x, y;  public: void set_values (int,int);  int area (void); } rect;   Declares a class (i.e., a type) called CRectangle and an object (i.e., a variable) of this class called  rect .  This class contains four members: two data members of type int (member x and member y) with private access (because private is the default access level) and two member functions with public access: set_values() and area(), of which for now we have only included their declaration, not their definition.
Notice the difference between the class name and the object name: In the previous example,  CRectangle  was the class name (i.e., the type), whereas  rect  was an object of type CRectangle.  It is the same relationship  int  and  a  have in the following declaration:  int a;  where  int  is the type name (the class) and  a  is the variable name (the object).
After the previous declarations of  CRectangle  and  rect , we can refer within the body of the program to any of the public members of the object  rect  as if they were normal functions or normal variables, just by putting the object's name followed by a dot (.) and then the name of the member. All very similar to what we did with plain data structures before. For example:  rect.set_values (3,4);  myarea = rect.area();
#include <iostream.h>  class CRectangle { int x, y;  public:  void set_values (int,int);  int area () {return (x*y);}  };  void CRectangle::set_values (int a, int b) {  x = a;  y = b;  }  void main () {  CRectangle rect;  rect.set_values (3,4);  cout << &quot;area: &quot; << rect.area(); }

More Related Content

PDF
Lec 6 14_aug [compatibility mode]
PPTX
PPTX
Array within a class
PPT
C++ classes
PPT
Lecture5
PPTX
C# Generics
PPT
C++lecture9
PDF
Introduction to C++
Lec 6 14_aug [compatibility mode]
Array within a class
C++ classes
Lecture5
C# Generics
C++lecture9
Introduction to C++

What's hot (20)

PPTX
Functions, classes & objects in c++
PPTX
PDF
Algorithms: I
PDF
Structures in c++
PDF
Function overloading
DOCX
C++ Template
PPTX
Operators in java
PPTX
Chapter2 array of objects
PPT
البرمجة الهدفية بلغة جافا - مصفوفة الكائنات
PPT
java tutorial 3
PPTX
Templates presentation
PDF
Class and object
PPT
C++ oop
PDF
PPTX
PPT
Java Concepts
DOCX
PPTX
C# Delegates
PPTX
class and objects
Functions, classes & objects in c++
Algorithms: I
Structures in c++
Function overloading
C++ Template
Operators in java
Chapter2 array of objects
البرمجة الهدفية بلغة جافا - مصفوفة الكائنات
java tutorial 3
Templates presentation
Class and object
C++ oop
Java Concepts
C# Delegates
class and objects
Ad

Viewers also liked (20)

PDF
Digital Arts & IT Classes for September-October @UNMCE
PPTX
Personal Good Practices by Daniela Simmons
PDF
Sonja holverson publications 2010
PDF
(Lucagbo) lab 1 1 konas espresso coffee annual cost of goods-1 sheet1
PDF
(Lucagbo) lab 1 1 konas espresso coffee annual cost of goods-1 sheet1
PDF
대신리포트_대신브라우저_150407
PPTX
Chapter one
PDF
How to Fire an Employee Without Risking Your Life
PDF
LEA Workshop 11/12/2013
PDF
"Beginners' Manual on Islam" (3) -Appendix 5 To Pg 155
PDF
AircraftIT MRO Journal Vol 3.2
PPTX
Master copy
KEY
Gameful Learning - Using games & game strategies to engage learners
PPTX
Photo Interview: A first time mom
PDF
MGMM FinOver00
PDF
Why Innovation Itself Must Be The Next Big Thing
PPTX
Lab school
PDF
A World That Is Turning Green
PDF
National Preparedness System component: About TractorFax
PPTX
Can laughing at our problems actually help us solve them?
Digital Arts & IT Classes for September-October @UNMCE
Personal Good Practices by Daniela Simmons
Sonja holverson publications 2010
(Lucagbo) lab 1 1 konas espresso coffee annual cost of goods-1 sheet1
(Lucagbo) lab 1 1 konas espresso coffee annual cost of goods-1 sheet1
대신리포트_대신브라우저_150407
Chapter one
How to Fire an Employee Without Risking Your Life
LEA Workshop 11/12/2013
"Beginners' Manual on Islam" (3) -Appendix 5 To Pg 155
AircraftIT MRO Journal Vol 3.2
Master copy
Gameful Learning - Using games & game strategies to engage learners
Photo Interview: A first time mom
MGMM FinOver00
Why Innovation Itself Must Be The Next Big Thing
Lab school
A World That Is Turning Green
National Preparedness System component: About TractorFax
Can laughing at our problems actually help us solve them?
Ad

Similar to 3 functions and class (20)

PPT
Classes & objects new
PPTX
Chapter 2 OOP using C++ (Introduction).pptx
PPTX
Chapter 13 introduction to classes
PPT
Classes in C++ computer language presentation.ppt
PPTX
OOC MODULE1.pptx
PPTX
Lecture 4. mte 407
DOC
My c++
PPTX
oop lecture 3
PPT
Classes, objects and methods
PPT
Object and class
PPT
Lecture 2
PDF
Class and object in C++ By Pawan Thakur
PDF
PPTX
Lecture 2 (1)
PPT
classes data type for Btech students.ppt
DOCX
Bc0037
PPT
DS Unit 6.ppt
PDF
chapter-7-classes-and-objects.pdf
PDF
CLASSES, STRUCTURE,UNION in C++
PPT
Unit vi(dsc++)
Classes & objects new
Chapter 2 OOP using C++ (Introduction).pptx
Chapter 13 introduction to classes
Classes in C++ computer language presentation.ppt
OOC MODULE1.pptx
Lecture 4. mte 407
My c++
oop lecture 3
Classes, objects and methods
Object and class
Lecture 2
Class and object in C++ By Pawan Thakur
Lecture 2 (1)
classes data type for Btech students.ppt
Bc0037
DS Unit 6.ppt
chapter-7-classes-and-objects.pdf
CLASSES, STRUCTURE,UNION in C++
Unit vi(dsc++)

3 functions and class

  • 1. C++ Review: FUNCTIONS
  • 2. VOID FUNCTIONS #include <iostream.h> void addoreven(); int main() { addoreven(); cout << &quot;That's all!\n&quot;; return 0; } void addoreven() { int your_number; cout << &quot;Enter a whole number: &quot;; cin >> your_number; if (your_number % 2 == 0) cout << &quot;Your number is even\n&quot;; else { cout << &quot;Your number is odd.\n&quot;; } }
  • 3. FUNCTIONS that RETURN VALUES #include <iostream.h> #include <conio.h> // function square computes the square of x int square (int x) { return x * x; } // main is where program execution begins void main () { int i; for (i = 0; i <= 20; i = i + 1) { cout << &quot;\nvalue = &quot; << i << &quot; square = &quot; << square(i); } // wait for key pressure getch(); }
  • 4. PROGRAMMING EXERCISE: Using Functions that return values, create a program that converts Farenheit values from 2 to 222 (incremented by 2) to Celsius To convert into Celsius, the following conversion factor is used: Celsius = 5/9 * (Farenheit – 32)
  • 5. #include <iostream.h> float celsius(float); int main() { for (int x=2; x<232; x += 10) cout << x << &quot;\t&quot; << celsius(x) << &quot;\n&quot;; } float celsius(float x) { return 5.0/9.0*(x-32); }
  • 6. PROGRAMMING EXERCISE: Using Functions that return values, create a program that displays the factorial of 0 to 10. Factorial example: 5! = 5 * 4 * 3 * 2 * 1
  • 7. PROGRAMMING EXERCISE: Using Functions that return values, create a program that reverses a given input string e.g. Rhej Cachero Output: orehcaC jehR
  • 8. Class an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. a new kind of data type; each class that you define is a collection of data ( data members) plus the ability to include special functions (member functions).
  • 9. Class An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.
  • 10. Classes are generally declared using the keyword class, with the following format: class class_name { access_specifier_1: member1; access_specifier_2: member2; ... } object_names; Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the declaration can contain members, that can be either data or function declarations, and optionally access specifiers. Class
  • 11. Class An access specifier is one of the following three keywords: private , public or protected . These specifiers modify the access rights that the members following them acquire: private members of a class are accessible only from within other members of the same class public members are accessible from anywhere where the object is visible.
  • 12. Class By default, all members of a class declared with the class keyword have private access for all its members . Therefore, any member that is declared before one other class specifier automatically has private access.
  • 13. Class class CRectangle { int x, y; public: void set_values (int,int); int area (void); } rect; Declares a class (i.e., a type) called CRectangle and an object (i.e., a variable) of this class called rect . This class contains four members: two data members of type int (member x and member y) with private access (because private is the default access level) and two member functions with public access: set_values() and area(), of which for now we have only included their declaration, not their definition.
  • 14. Notice the difference between the class name and the object name: In the previous example, CRectangle was the class name (i.e., the type), whereas rect was an object of type CRectangle. It is the same relationship int and a have in the following declaration: int a; where int is the type name (the class) and a is the variable name (the object).
  • 15. After the previous declarations of CRectangle and rect , we can refer within the body of the program to any of the public members of the object rect as if they were normal functions or normal variables, just by putting the object's name followed by a dot (.) and then the name of the member. All very similar to what we did with plain data structures before. For example: rect.set_values (3,4); myarea = rect.area();
  • 16. #include <iostream.h> class CRectangle { int x, y; public: void set_values (int,int); int area () {return (x*y);} }; void CRectangle::set_values (int a, int b) { x = a; y = b; } void main () { CRectangle rect; rect.set_values (3,4); cout << &quot;area: &quot; << rect.area(); }