CS1201: Programming Language 2

Slides:



Advertisements
Similar presentations
1 Chapter 11 Introducing the Class Pages ( )
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
Information Hiding Chapter 5: Classes and Objects in Depth.
1 Writing a Good Program 5. Objects and Classes in C++
Introduction to methods Chapter 5: Classes and Objects in Depth.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Objects and Instance attributes and variables Chapter 3: Introduction to Classes and Objects.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Chapter 10 METHODS AND CONSTRUCTORS 1. Accessing Objects  Referencing the object’s data: objectReference.data myCircle.radius  calling the object’s.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 Chapter 13 Introduction to Classes. 2 Topics 12.1 Procedural and Object-Oriented Programming 12.2 Introduction to Classes 12.3 Defining an Instance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 10 Introduction to Classes
CS1201: Programming Language 2 Classes and objects By: Nouf Aljaffan Edited by : Nouf Almunyif.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
1 CSC241: Object Oriented Programming Lecture No 02.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
CS1201: Programming Language 2 Classes and objects.
Classes and Objects. Object Software objects are modeled after real-world objects in that they, too, have state and behavior. A software object maintains.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Classes and Objects. How can one design a program?  Top-down structured design: uses algorithmic decomposition where each module denotes a major step.
Lecture #6 Classes and Objects.
Classes.  A collection of variables combined with a set of related functions MemberFunctions (methods) (methods) MemberVariables (data members) (data.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
Classes and Objects. Object vs. Class Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2  A class could be considered as a set of objects having.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
OBJECT ORIENTED PROGRAMMING INTRODUCTION. WHAT IS OBJECT-ORIENTATION ALL ABOUT? Principles and techniques for system modeling which: Aim to produce a.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Introduction to Object-oriented Programming
Procedural and Object-Oriented Programming
By Muhammad Waris Zargar
Object Oriented Programming
Review: Two Programming Paradigms
Introduction to Classes
Concepts and Basics of C++ Programming
Introduction to Object-oriented Programming
Chapter 5 Classes.
CS1201: Programming Language 2
This technique is Called “Divide and Conquer”.
CS1201: Programming Language 2
Concepts and Basics of C++ Programming
C++ Classes & Object Oriented Programming
Pointer Data Type and Pointer Variables
Introduction to Classes
CS1201: Programming Language 2
Programming -2 برمجة -2 المحاضرة-7 Lecture-7.
Chapter 5: Classes and Objects in Depth
Chapter 5: Classes and Objects in Depth
Introduction to Classes and Objects
Chapter 5: Classes and Objects in Depth
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
Types of Computer Languages
Lecture 8 Object Oriented Programming (OOP)
Object-Oriented Design AND CLASS PROPERTIES
Introduction to Classes and Objects
Presentation transcript:

CS1201: Programming Language 2 By:Nouf Aljaffan Edited by : Nouf Almunyif Classes and objects

Object You can look around you now and see many examples of real-world objects: your cat, your desk, your television set, your bicycle. These real-world objects share two characteristics: they all have state and they all have behavior For example, dogs have state (name, color, breed, hungry) and dogs have behavior (barking, fetching).

Object Software objects are modeled after real-world objects in that they, too, have state and behavior. A software object maintains its state in variables and implements its behavior with methods. An object combines data and operations on the data into a single unit.

CLASSES The Class is the foundation of C++ support for the OOP (Object-Oriented Programming ). It is the core of many of its more advanced features. In C++, the mechanism that allow you to combine data and operations on the data into a single unit is called a class. A class is a collection of fixed number of components. The components of a class are called the members of the class.

CLASSES class definition: classMembersList consists of variable (attribute) and/or functions (method) declarations . class classIdentifier //name { classMembersList };

Members Declaration Data : functions: Each class containing data members and member functions Data : If a member of a class is a variable It is declared like any other variable You cannot initialize a variable when you declare it functions: If a member of a class is a function Only Function prototype is listed. Or, the body of a member function is defined inside a class declaration, it is declared inline.

CLASSES class car //name { string color ; String model; Double price ; Void print (){ Cout <<“ the car information “<< color <<model<< price<< endl;) }; classMembersList consists of variable (attribute) and/or functions (method) declarations .

Cont. Classes Members of a class are classified into one of three categories: Private (default) : not accessible outside the class. Protected: not accessible outside the class. Public: accessible outside the class. Note: The keyword of category name is followed by colon (:) . In the definition of a class, you cannot initialize a variable when you declare it. In C++, a class is a definition. No memory is allocated for the class itself; memory is allocated for the class objects (class instances) when you declare them.

Define a Class Type class Rectangle { private: float width; float length; public: void setWidth(float w); void setLength(float l); float calcArea(); }; Header class class_name { permission_label: member; ... }; Data Members Body Member Functions

Accessing Class Members Operators to access class members Dot member selection operator (.) Object Reference to object Example: Rectangle r; r.setWidth(3);

Example }; Char gender; Void main( )‏ { Cat MyCat; MyCat.setAge(3); MyCat.setWeight(2); MyCat.gender=“m” cout << "My cat\‘ s age is " << MyCat.getAge(); cout << " and weighs " << MyCat.getWeight() << " kg\n“ ; cout << “and its gender “ << MyCat.gender; } #include <iostream> using namespace std; class Cat { private: int age; int weight; public: Char gender; void setAge (int yrs) { age = yrs; } void setWeight (int kgs) { weight = kgs; } int getAge() { return age; } int getWeight() { return weight; } };

Accessibility Example1 … Service obj; obj.memberOne = 10; obj.memberTwo = 20; obj.doOne(); obj.doTwo(); class Service { private: int memberTwo; void doTwo() {……} public: int memberOne; void doOne() {…….}}; This is a simple example of public and private modifiers. See how the client can access the public data member and method. Main Program Service Class

Accessibility Example2 #include <iostream> course2.studentName= "Salwa AlAmri"; #include <string> cout<< course1.studentName << " has the course "<<course1.courseCode<< endl; using namespace std; class Course { cout<<course2.studentName << " has the course "<<course2.courseCode<< endl; // Data Member public: } string studentName; string courseCode ; }; void main() { Course course1, course2; // assign values to course1 course1.courseCode= "CSC1201"; course1.studentName= "Muna AlKebir"; //assign values to course2 course2.courseCode= "csc1301";

Accessibility Example 3 #include <iostream> #include <string> using namespace std; class Course { private: string studentName; string courseCode ;}; void main() { Course course1, course2; //assign values to course1 course1.courseCode= “CSC1201“; course1.studentName= “Muna AlKebir“; //assign values to course2 course2.courseCode= “CSC1301“; course2.studentName= “Salwa AlAmri“; cout<<course1.studentName << " has the course “<<course1.courseCode<<endl; cout<<course2.studentName << " has the course “<< course2.courseCode<<endl; }

Constructors Vs. Destructor Constructors guarantee that the member variables are initialized when an object is declared. Constructors automatically execute when a class object enters its scope. The name of a constructor is the same as the name of the class. A class can have more than one constructor. A constructor without parameters is called the default constructor. Destructor automatically execute when a class object goes out of scope. The name of a destructor is the tilde (~), followed by the class name (no spaces in between). A class can have only one destructor. The destructor has no parameters. Constructors and Destructor are functions without any type. As a result, they cannot be called like other functions. Note : A function can return a value of type class.

Example # 1 #include <iostream> using namespace std; class Circle { private: float radius; public: //prototype only ! // constructors Circle(){radius=0;} Circle(float r){ setRadius(r); } // destructor ~Circle(){cout<<" ending object...\n"; } void setRadius(float r){ if ( r >=0.0)‏ radius=r; else radius=0.0; } float getRadius(){return radius; } float area(){return 3.14*radius*radius;} float perimeter(){return 2 * 3.14 * radius;} };

Example # 1 int main()‏{ float x; cout<<" Enter the radius of the circle: "; cin>>x; Circle C1(x); cout<<"\n the area of the circle is: "<<C1.area()<<endl; cout<<" and the perimeter is:"<<C1.perimeter()<<endl; return 0; }

Implementing class Functions There are two ways: Member functions defined outside class: Using Binary scope resolution operator (::) ReturnType ClassName::MemberFunctionName( ){..} Member functions defined inside class When the body of a member function is defined inside a class declaration, it is declared inline.

Example #1

Output

Example

Example

Example

Example

Output: