C++ is about classes A class is a building block, a self- contained entity providing public data fields (properties) and public operations (methods) for.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Inheritance In C++  Inheritance is a mechanism for building class types from other class types defining new class types to be a –specialization –augmentation.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Lecture 9 Concepts of Programming Languages
IT PUTS THE ++ IN C++ Object Oriented Programming.
1)Never start coding unless you understand the task! 2)Gather requirements first. This means identify the problem and ask questions about it. Now you kind.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Programming Languages and Paradigms Object-Oriented Programming.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
PRESENTED BY, P.S.S.SWAMY..  What is an application package.  What is an application class.  Object oriented concepts.  Understanding application.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Object Oriented Software Development
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Introduction to Object-Oriented Programming Lesson 2.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
ISBN Chapter 12 Support for Object-Oriented Programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
C# for C++ Programmers 1.
Object-Oriented Programming (OOP) Lecture No. 45
Inheritance and Polymorphism
Object-Oriented Programming
Review: Two Programming Paradigms
Introduction to Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Introduction to Classes
Java Programming Language
CPS120: Introduction to Computer Science
Lecture 10 Concepts of Programming Languages
Final Exam Review Inheritance Template Functions and Classes
SPL – PS3 C++ Classes.
Presentation transcript:

C++ is about classes A class is a building block, a self- contained entity providing public data fields (properties) and public operations (methods) for manipulating the class. From the developer’s stand-point a class may contain private data and private methods to be used internally. C++ Classes

Abstraction: classes make difficult tasks look simple by hiding unnecessary detail from user. Example: a car is an abstract concept (motor and electronics are hidden beneath the hood) Polymorphism: different classes can share programming interface, which allows programmer to use different classes interchangeably but with different effect. Example: shipping companies are “polymorphic”, it is sufficient to fill-out sender and recipient address to send a package with any company. From user’s standpoint polymorphism provides interchangeability without the regard to internal differences (e.g. you can use a heat pump with your house’s air handler regardless of the heat pump being electric, gas or geothermal) It is the developer’s responsibility to provide the internal details of polymorphic classes while adhering to the unified interface. Advantages of Classes

class MyClass { public: MyClass(…); // constructors ~MyClass(); // destructor (optional) public: int MyProperty; // properties public: void MyMethod(…); // methods private: // hidden under-the-hood int InternalData; // details void InternalMethod(…) }; C++ Class Example

Object is an instance of a class. Allocating an object on stack: MyClass myClass(…); class object Allocating and object on heap: MyClass* pMyClass = new MyClass(…); To avoid memory leaks allocate your objects on stack wherever you can! Accessing members: myClass.MyMethod(…); myClass.MyProperty = 1; pMyClass->MyMethod(…); pMyClass->MyProperty = 1;Objects

Constructors: called when an object is constructed on stack, allocated on heap using the new operator, or copied (copy-construction) Destructors: called when a stack-allocated object goes out of scope or heap-allocated object is deleted using the delete operator. Operators: some classes override +,-,=, >, etc. operators for convenience. Do not override operators unless you are absolutely have to! There are very few cases (aside from mathematical objects) that warrant operator overloading. Constant Fields: constant properties. Superclass (base class): a subclass can be derived from a parent base class kind of like a child can be born from a parent; both a alike yet differ in some respects. Virtual Members: base class members that are meant to be overloaded (i.e. replaced) in a subclass. Protected Members: private members that can be accessed from the subclass’ code. Abstract Methods (Pure Virtual Methods): methods that totally lack implementation. What a Class Can Contain?

Const Methods do not modify the object’s internal state (and thus cannot call non-const methods internally): class MyClass { public: MyClass(…); public: void MyMethod(…) const; }; Always define a method as const if it does not modify the object’s internal data. Const Methods

A class can contain static data members and static methods class MyClass { public: MyClass(…); public: static int MyProperty; static int MyStaticProperty; static MyStaticMethod(…); // Can reference MyStaticProperty but cannot MyProperty }; Static methods are global in the sense that they do not apply to an instance but apply to the class. Therefore static methods can reference only static data members of the class and cannot reference instance properties. // Invocation example MyClass::MyStaticMethod(); Static Members

Pass parameters as const reference: class MyClass { public: MyClass(…); public: void MyMethod(const SomeOtherType&); }; Passing parameter by reference eliminates copy-constructor overhead: if an object is passed by value a copy of the object is constructed on stack. Parameter Passing

Define as many constructors as necessary. Constructors should match ways to initialize an object (and accept all necessary parameters for object initialization). class DateTime { public: DateTime(); // Default constructor, today’s date & time DateTime(int year, int month, int day); DateTime(int year, int month, int day, int hour, int minute, int second); };Constructors

Only one destructor. Destructor is needed if the class allocates resources (i.e. heap memory, files, OS handles, etc.) that need to be disposed / released when the class is destroyed. Protected destructors: required subclasses to declare their own public destructors. class MyClass { public: MyClass(); ~MyClass(); };Destructors

To specialize a generic class one can derive his one class from the generic base class. Virtual methods provide such ‘specialization points’. class MyBaseClass { public: MyBaseClass(); virtual void MyVirtualMethod(); }; class MyDerivedClass: public MyBaseClass { public: MyDerivedClass(); virtual void MyVirtualMethod(); // overrides MyBaseClass::MyVirtualMethod }; In order to override a virtual method its signature in the derived class must match exactly the base class method. Derived class can contain new members. Derived class can access public and protected members of its base class(es). Base Classes, Inheritance

When you define a base class but cannot prove the default implementation for one or more virtual methods you should declare such methods as pure virtual: class MyBaseClass { public: MyBaseClass(); virtual void MyVirtualMethod() = 0; }; Classes containing pure virtual methods are called abstract. Abstract classes cannot be instantiated and thus must serve as base classes only. Abstract Classes, Pure Virtual

Interfaces can be viewed as pure “abstract classes” in the sense that they support member declarations only and does not allow any method implementations. Interfaces are not a standard feature in C++, yet existing programming paradigms require their support, hence Microsoft’s __interface extension: __interface MyInterface { void MyMethod(); }; *Interfaces define only public members! *Interfaces cannot contain constructors, destructors or data members, but can inherit from one or more base interface.Interfaces

Separate class declaration from implementation. Put class declaration in.h file, implementation in.cpp file. Keep your class structure rigid. Inline small methods only. Do not provide get/set accessors for trivial read/write properties. Keep one large class per header file. It is OK to group a bunch of small related classes in a single.h and.cpp pair if your project deals with a large number of classes. General Guidelines

Separate class declaration from implementation. Put class declaration in.h file, implementation in.cpp file. Keep your class structure rigid. Inline small methods only. Do not provide get/set accessors for trivial read/write properties. Keep one large class per header file. It is OK to group a bunch of small related classes in a single.h and.cpp pair if your project deals with a large number of classes. General Guidelines

Class Structure Example

C++ allows overriding operators such as +, -,, > that can serve special purpose: class MyClass { public: MyClass(); public: bool operator = (const MyClass&) const; MyClass operator + (const MyClass&) const; }; From developers stand point operators are methods with a specific name that accept specific parameters. C++ supports stand-alone operators that are not members of any class, e.g. MyClass operator + (const MyClass& a, const MyClass& b); As a general rule you should NOT overload operators unless developing a math-related class. Operators are NOT intuitive and although they are cool understanding operator-rich program is difficult.Operators

1) Create class; 2) Disentangle from input/output; 3) Extract local part, domain; 4) Check validity - Validate method - level1: basic structure, local (64) & domain part (255) lengths - level2: check for invalid characters and combinations - level3: TLD within the defined list 5) Use exceptions 6) Define IsValid property.Exercise

class { public: (); (char* ); (char* localPart, char* domain); public: char* GetLocalPart() const; char* GetDomain() const; char* GetTLD() const; bool IsValid() const; public: virtual void Parse(char* , int validityLevel); // throws exception virtual void Parse(int validityLevel); // throws exception private: char* LocalPart; char* Domain; char* TLD; };Example

1) Operators: >> Operator for initializing instance from stream << Operator for writing instance to streamAssignment