Object-Oriented Programming C programmers concentrate on writing functions.functions By contrast, MC++ programmers concentrate in creating their own user-defined.

Slides:



Advertisements
Similar presentations
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
 2002 Prentice Hall. All rights reserved. Some slides modified by LL 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base.
Properties Methods of a class can manipulate that class’s private data members Methods of a class can manipulate that class’s private data members Classes.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
Inheritance Is a form of software reusability in which programmers create classes that absorb an existing class’s data and behaviors and enhance them with.
1 C++ Structures Starting to think about objects...
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
Review of C++ Programming Part II Sheng-Fang Huang.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Visual Programming Fall 2012 – FUUAST Topic: Development environment.
CLASS INHERITANCE WEEK 7 Chapter 20 CS SENEM KUMOVA METIN1.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2 Base Classes and Derived.
Microsoft Visual C++.NET Chapter 51 Introduction to Classes.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
Inheritance & Polymorphism1. 2 Introduction Besides composition, another form of reuse is inheritance. With inheritance, an object can inherit behavior.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Advanced Programming Class & Objects. Example Create class account to save personal account for a bank. The class has account# and balance data The class.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Object Oriented Programming
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CITA 342 Section 1 Object Oriented Programming (OOP)
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Jozef Goetz contribution,  2011 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Jozef Goetz contribution,  2011, 2014 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved.
Inheritance ndex.html ndex.htmland “Java.
Object-Oriented Programming: Inheritance and Polymorphism.
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
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.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
CompSci 230 S Programming Techniques
Inheritance ITI1121 Nour El Kadri.
Chapter 9 – Object-Oriented Programming: Inheritance
Classes & Objects.
C++ Classes & Object Oriented Programming
The University of Texas Rio Grande Valley
Can perform actions and provide communication
Can perform actions and provide communication
Week 6 Object-Oriented Programming (2): Polymorphism
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Chapter 9 - Object-Oriented Programming: Inheritance
Can perform actions and provide communication
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look UTPA – Fall 2012 This set of slides is revised from lecture.
CS360 Client/Server Programming Using Java
CPS120: Introduction to Computer Science
Presentation transcript:

Object-Oriented Programming C programmers concentrate on writing functions.functions By contrast, MC++ programmers concentrate in creating their own user-defined types, called classes. classes When building an MC++ application, each class definition normally is placed in a header file, and that class’s method definitions are placed in source-code files with the same name as the header but extension.cpp

An example – define a managed class (header file) #pragma once // #using using namespace System; public __gc class Time { public: Time(); void SetTime(int, int, int); String *ToUniversalString(); String *ToStandardString(); private: int hour; int minute; int second; };

Time::Time() { hour =0; minute = 0; second = 0; } Void Time::SetTime( int h, int m, int s) { hour = h; minute = m; second = s; } String *Time1::ToUniversalString() { return String::Concat(hour.ToString(S”D2”), S”:”, minute.ToString(S”D2”), S”:”, second.ToString(S”D2”)); } String *Time1::ToStandardString() { return String::Concat( ((hour == 12 || hour == 0) ? 12 : hour %12).ToString(), S”:”, minute.ToString(S”D2”), S”:”, second.ToString(S”D2”), S” “, (hour<12? S”AM” : S”PM”)); } An example – method definition (implementation)

#pragma once directive instructs the compiler to process a header file only once even if it is included in more than one file in a project. Class Time1 implicitly inherits from class Object. Every class in MC++(Except Object) inherits from an existing class definition Keyword __gc specifies that class Time1 is a managed class. Because class Time is managed class, all objects of this class must be created with new operator Use String method Concat to format the minutes as two-digit values with leading zeros if necessary Class MessageBox create a dialog. Dialogs are windows that typically display important messages to the user of an application. Class dialog is defined in namespace System::Windows::Forms

An example- use Time class #include<stdafx.h” #include”Time.h” #using using namespace System; using namespace System::Windows::Forms; int _tmain() { Time * t = new Time(); String *output; output = String::Concat(S”Initial univesal time is”, t->ToUniversalString()); t->setTIme( 10, 30,0); output = String::Concat(output, S”\nUniversal time after SetTime is: “, t->ToUniversalString()); MessageBox::Show(output, S”Tsting class Time”); return 0; }

properties Methods of a class can manipulate that class’s private data members Classes provide public properties to allow clients to set ( assign value to) or get(obtain value of) private data members __property void set_Hour( int val) { hour = (val >=0 &&val<24)?val:0); } __property int get_Hour() { return hour; }

properties Providing set and get capabilities appears to be the same as making the variables public Every property method name must be preceded by either set_ or get_ __property void set_propertyName() PropertyName specifies the name of property. Hour is scalar property Scalar property is property that can be accessed like variables. We assign value to scalar properties using = operator. When assignment occurs, the code in the set accessor for that property executes Time *time = new Time(); time->Hour = Int32::Parse(Console::ReadLine());

Static members Static variable: all class objects share only one copy of a static variable. Static variable represents class-wide information Declaration static in count; Static member can be initialized in its declaration Public static data can be accessed from client code through the class name using the scope resolution operator:: The private static data can be accessed only through methods or property of the class

Static methods To enable a client to access a private static member when no objects of the class exists, the class must provide a public static method or property A static method cannot access non-static data Static variable and static methods exist even when there are no objects of that class in which the static members are defined

OOP- inheritance Inheritance is one of main features of Object-Oriented Programming Inheritance is a form of software reusability on which classes are created by absorbing an existing class’s data and behaviors and embellishing them with new capabilities When creating a class, instead of writing completely new data members and methods, the programmer can designate that the new class should inherit the data members, properties and methods of another class. The previous defined class is called base class, and the new class is referred to as the derived class other language, Java refer to base as superclass and derived class as subclass

Point class public __gc class Point { public: Point(); Point(int, int); __property int get_X(){return x;} __property void set_X(int val){x = val;} __property int get_Y(){ return y;} __property void set_Y(int val){ y = val;} String *ToString(); private: int x,y; };

Implementation String * Point::ToString() { return String::Concat(S”[“,x.ToString(), S”,”,y.ToString(), S”]”); } Point::Point(){ } Point::Point( int xval, int yval) { x = xval; y = yval; }

Circle – inherits Point class public __gc class Circle :public Point { public: Circle(); Circle( int,int,double); __property double get_Radius(){return radius;} __property void set_Radius(double r) {if( r > 0) radius = r;} double Diameter(); double Area(); double Circumference(); String *ToString(); private: double radius; };

Circle Circle::Circle(){} Circle::Circle(int xval,int yval, double r) :Point(xval,yval) { radius = r;} double Circle::Diameter(){ return radius *2;} double Circle::Circumference() { return Math::PI *Diameter(); } double Circle::Area() { return Math::PI * Math::Pow(radius, 2);} String *Circle::ToString() { return String::Concat(S”center= “, __super::ToString(), S”;Radius = “, Radius.ToString()); }

Sample client code int _tmain() { Circle *cir = new Circle(4,5,2.5); String *output = String::Concat(S”X coordinate is “, cir->X.ToString(), S”\nY coordinate is “, cir->Y.ToString(), S”\nRadius is “, cir->Radius.ToString()); return 0; }

Practice Define a class called Cylinder that inherits from Circle. Has data member height Methods: Volume() that return volume of the cylinder Area() return the cylinder’s surface area Properties: set_Height and get_Height

Use the following client code to test your class definition int _tmain() { Cylinder *cyn = new Cylinder(12,23,2.5,5.7); String *out = String::Concat (S"X coordinate is ", cyn->X.ToString(), S"\nY coordinate is ", cyn->Y.ToString(),S"\nRadius is ", cyn->Radius.ToString(),S"\nHeight is ", cyn->Height.ToString()); out = String::Concat (out, S"\nDiameter is ", cyn->Diameter().ToString(S"F"), S"\n"); out = String::Concat (out, S"Volume is", cyn->Volume().ToString(S"F")); MessageBox::Show (out, S"Demonstrating class Cylinder"); return 0; }