Overview of Previous Lesson(s) Over View .NET Framework is a software framework developed by Microsoft that runs primarily on Microsoft Windows.  It.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Destructors Math 130 Lecture # xx Mo/Da/Yr B Smith: New lecture for 05. Use this for evolving to Java B Smith: New lecture for 05. Use this for evolving.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 2 Introduction to C Programming
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Chapter 15: Operator Overloading
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Structuring Your Data Using Classes Chapter 8. What We’ll Cover in Chapter 8 Classes and how they are used The basic components of a class How a class.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
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 15: Overloading and Templates.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
1 Chapter 7 Defining Your Own Data Types. 2 What Is a struct ?  A structure is a user-defined type You define it using the keyword struct so it is often.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Overview of Previous Lesson(s) Over View  Operator Overloading:  Operator overloading is a very important capability, it enables to make standard C++
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Chapter 10 Introduction to Classes
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Virtual FunctionstMyn1 Virtual Functions A virtual function is declared in a base class by using the keyword virtual. A function that you declare as virtual.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Overloading operators C++ incorporates the option to use standard operators to perform operations with.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Overview of Previous Lesson(s) Over View VP is the methodology in which development allows the user to grab and use the desired tools like menus, buttons,
Programming Fundamentals Enumerations and Functions.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Overview of Previous Lesson(s) Over View 3  CLR Programming  Common Language Runtime (CLR) is a programming, that manages the execution of programs,
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.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Bill Tucker Austin Community College COSC 1315
LESSON 06.
User-Written Functions
論語 先進第十一 暮春者,春服既成 冠者五六人,童子六七人 浴乎沂,風乎舞雩,詠而歸 ㄧˊ 〔~河〕水名,源出中國山東省,至江蘇省入海。
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Introduction to C++ Systems Programming.
Java Primer 1: Types, Classes and Operators
Chapter 5 Classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to Classes and Objects
Classes and Objects.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Overview of Previous Lesson(s)

Over View .NET Framework is a software framework developed by Microsoft that runs primarily on Microsoft Windows.  It includes a large library and provides language interoperability across several programming languages 3

Over View..  Programs written for the.NET Framework execute in a software environment, known as the CLR, an application virtual machine that provides services such as security, memory management, and exception handling.  The class library and the CLR together constitute the.NET Framework. 4

Over View…  A pointer is a variable that stores the address of another variable of a particular type. long *pnum;//convention in C++  For getting the address & operator is used.  This is a unary operator that obtains the address of a variable.  It ’ s also called the reference operator. long number = 200; long* pnumber; pnumber = &number; 5

Over View… 6  Operator & can be used to obtain the address of any variable, but you need a pointer of the appropriate type to store it.

Class 7  A class is a specification of a data type that you define.  A class combines both the definition of the elementary data that makes up an object and the means of manipulating the data that belongs to individual objects of the class (functions).

Over View… 8  CBox data type using the keyword class is defined as follows: class CBox { public: double m_Length; // Length of a box in inches double m_Width; // Width of a box in inches double m_Height; // Height of a box in inches double Volume() {return m_Length*m_Width*m_Height; } };

Over View… 9 CBox box1; // Declare box1 of type CBox CBox box2; // Declare box2 of type Cbox

10

Contents  Class Constructor  Default Constructor / Default Copy Constructor  Initializer List  Private Members of a Class  Friend’s Functions  The pointer this  Destructor  C++/CLI Programs  C++/CLI Specific: Fundamental Data Types  C++/CLI Output 11

Class Constructor 12  A class constructor is a special function in a class that is responsible for creating new objects when required.  A constructor provides the opportunity to initialize objects as they are created and to ensure that data members only contain valid values.  A class may have several constructors, enabling you to create objects in various ways.

ExAMPLE 13 // Using a constructor #include Using namespace std; class CBox // Class definition at global scope { public: double m_Length; double m_Width; double m_Height;

Constructor Definition 14 // Constructor definition CBox(double lv, double bv, double hv) { cout << endl << “Constructor called.”; m_Length = lv; // Set values of data members m_Width = bv; m_Height = hv; } // Function to calculate the volume of a box double Volume() {return m_Length* m_Width* m_Height;} };

Main Function 15 int main() { CBox box1(78.0,24.0,18.0); // Declare and initialize box1 CBox cigarBox(8.0,5.0,1.0); // Declare and initialize cigarBox double boxVolume(0.0); // Stores the volume of a box boxVolume = box1.Volume(); // Calculate volume of box1 cout << endl << “Volume of box1 = “ << boxVolume; cout << endl << “Volume of cigarBox = “<< cigarBox.Volume(); cout << endl; return 0; }

Default Constructor 16 CBox box2; // Declare box2 of type Cbox  Here, you ’ ve left box2 without initializing values.  When you rebuild this version of the program,  you get the error message:  error C2512: 'CBox': no appropriate default constructor available  A default constructor is one that does not require any arguments to be supplied, or one whose arguments are all optional. CBox() // Default constructor { } // Totally devoid of statements

Ex Default Constructor 17 class CBox // Class definition at global scope { public: double m_Length; double m_Width; double m_Height; // Constructor definition CBox(double lv, double bv, double hv) { cout << endl << “Constructor called.”; m_Length = lv; m_Width = bv; m_Height = hv; } // Default constructor definition CBox() {cout << endl << “Default constructor called.”; } // Function to calculate the volume of a box double Volume() {return m_Length*m_Width*m_Height; } };

Ex Default Constructor 18 int main() {CBox box1(78.0,24.0,18.0); // Declare and initialize box1 CBox box2; // Declare box2 - no initial values CBox cigarBox(8.0, 5.0, 1.0); // Declare and initialize cigarBox double boxVolume(0.0); // Stores the volume of a box boxVolume = box1.Volume(); // Calculate volume of box1 cout << endl<< “Volume of box1 = “ << boxVolume; box2.m_Height = box1.m_Height - 10; // Define box2 box2.m_Length = box1.m_Length / 2.0; // members in box2.m_Width = 0.25*box1.m_Length; // terms of box1 return 0; }

Output 19 Constructor called. Default constructor called. Constructor called. Volume of box1 = 33696

Default Copy Constructor 20  A no-argument constructor can initialize data members to constant values & a multi-argument constructor can initialize data members to values passed as arguments.  It can be initialized with an other object of the same type.  It’s called the default copy constructor.  It’s a one argument constructor whose argument is an object of the same class as the constructor. CBox box2(box1); CBox box2 = box1;

Intializer List 21  Previously the members of an object in the class constructor, were intialized using explicit assignment. // Constructor definition using an initialization list CBox(double lv = 1.0, double bv = 1.0, double hv = 1.0): m_Length(lv), m_Width(bv), m_Height(hv) {cout << endl << "Constructor called.";}  The way this constructor definition is written assumes that it appears within the body of the class definition.

Intializer List.. 22  The values of the data members are not set in assignment statements in the body of the constructor.  As in a declaration, they are specified as initializing values using functional notation and appear in the initializing list as part of the function header.  Syntax:  Note that the initializing list for the constructor is separated from the parameter list by a colon, and each of the initializers is separated by a comma.  The MFC also relies heavily on the initialization list technique.

Private Members 23  A constructor that sets the values of the data members of a class object, but still admits the possibility of any part of a program being able to mess with what are essentially the guts of an object, is almost a contradiction in terms.  Protection is needed for class data members.  Private Keyword:  Class members that are private can, in general, be accessed only by member functions of a class.  A normal function has no direct means of accessing the private members of a class.

Private Members.. 24

Private Members.. 25 class CBox // Class definition at global scope { public: // Constructor definition using an initialization list CBox(double lv = 1.0, double bv = 1.0, double hv = 1.0): m_Length(lv), m_Width(bv), m_Height(hv) { cout < < endl < < "Constructor called."; } // Function to calculate the volume of a box double Volume() { return m_Length*m_Width*m_Height; } private: double m_Length; // Length of a box in inches double m_Width; // Width of a box in inches double m_Height; // Height of a box in inches };

Private Members.. 26 int main() { CBox match(2.2, 1.1, 0.5); // Declare match box CBox box2; // Declare box2 - no initial values cout << endl << "Volume of match = “< < match.Volume(); // Uncomment the following line to get an error // box2.m_Length = 4.0; cout << endl << "Volume of box2 = “< < box2.Volume(); return 0; }

Friends Function 27  Functions outside the class be able to access all the members of a class, a sort of elite group with special privileges.  Such functions are called friend functions of a class and are defined using the keyword friend  Either include the prototype of a friend function in the class definition, or you can include the whole function definition. friend double BoxSurface(Cbox aBox);

The Pointer this 28  In the CBox class, Volume() function is defined in terms of the class member names in the definition of the class.  Of course, every object of type CBox that you create contains these members, so there has to be a mechanism for the function to refer to the members of the particular object for which the function is called.  When any member function executes, it automatically contains a hidden pointer with the name this, which points to the object used with the function call.

The Pointer this 29  When the member m_Length is accessed in the Volume() function during execution, its actually referring to this - > m_Length  The compiler takes care of adding the necessary pointer name this to the member names in the function.  If you need to, you can use the pointer this explicitly within a member function.

Destructor 30  As constructor is called automatically when a object is created, similarly there is another function, which is called automatically when an object is destroyed.  Such a function is called a destructor.  A destructor has the same name as the constructor (which is the same as the class name) but is preceded by a tilde: ~Cbox () {}

C++ / CLI Programs 31  C++/CLI provides a number of extensions and additional capabilities:  All the ISO/IEC fundamental data types can be used in a C++/CLI program, but with few add-ons.  C++/CLI provides its own mechanism for keyboard input and output to the command line in a console program.  C++/CLI introduces the safe_cast operator that ensures that a cast operation results in verifiable code being generated.  C++/CLI provides an alternative enumeration capability that is class - based and offers more flexibility than the ISO/IEC C++ enum declaration you have seen.

C++ / CLI Fundamental Data Types 32  ISO/IEC C++ fundamental data type names can be used in C++/CLI programs, and with arithmetic operations.  The fundamental type names in a C++/CLI program have a different meaning and introduce additional capabilities in certain situations.  A fundamental type in a C++/CLI program is a value class type and can behave either as an ordinary value or as an object if the circumstances require it.  ISO/IEC C++ fundamental data type names can be used in C++/CLI programs, and with arithmetic operations.  The fundamental type names in a C++/CLI program have a different meaning and introduce additional capabilities in certain situations.  A fundamental type in a C++/CLI program is a value class type and can behave either as an ordinary value or as an object if the circumstances require it.

C++ / CLI Fundamental Data Types.. 33  Within the C++/CLI language, each ISO/IEC fundamental type name maps to a value class type that is defined in the System namespace.  The fundamental types, the memory they occupy, and the corresponding value class types are shown next:

C++ / CLI Fundamental Data Types.. 34

C++ / CLI Fundamental Data Types  Because the ISO/IEC C++ fundamental type names are aliases for the value class type names in a C++/CLI program, in principle, you can use either in your C++/CLI code.  Ex ISO/IEC C++ int count = 10; double value = 2.5;  But in C++ / CLI, it is like this: System::Int32 count = 10; System::Double value = 2.5;

C++ / CLI Output 36 #include "stdafx.h" using namespace System; int main(array ^args) { int apples, oranges; // Declare two integer variables int fruit; //...then another one apples = 5; oranges = 6; // Set initial values fruit = apples + oranges; // Get the total fruit Console::WriteLine(L"\nOranges are not the only fruit..."); Console::Write(L"- and we have "); Console::Write(fruit); Console::Write(L" fruits in all.\n"); return 0; }

C++ / CLI Output Format.. 37  The Write() and WriteLine() function is a C++/CLI function is defined in the Console class in the System namespace.  Both the Console::Write() and Console::WriteLine() functions have a facility for you to control the format of the output, and the mechanism works in exactly the same way with both. int packageCount = 25; Console::WriteLine(L"There are {0} packages.", packageCount);

C++ / CLI Output Format  The arguments that follow the first argument to the Console::WriteLine() function are numbered in sequence starting with zero, like this:  Thus, the zero between the braces in the previous code fragment indicates that the value of the packageCount argument should replace the {0} in the string that is to be written to the command line.

Thank You 39