انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة
الكلية كلية العلوم للبنات
القسم قسم الحاسبات
المرحلة 2
أستاذ المادة احمد علي حسين الجنابي
13/12/2016 10:30:22
Classes and Objects So far you have explored the structure of a simple program that starts execution at main() and enables you to declare local and global variables and constants and branch your execution logic into function modules that can take parameters and return values. All this is very similar to a procedural language like C, which has no object- orientation to it. In other words, you need to learn about managing data and connecting methods to it. In this lesson, you will learn ? What classes are ? How classes help you consolidate data with methods (akin to functions) that work on them ? About constructors and destructors ? Object-oriented concepts of encapsulation and abstraction ? What this pointer is about Declaring a Class Declaration of a class involves the usage of keyword class followed by the name of the class, followed by a statement block {…} that encloses a set of member attributes and methods within curly braces, finally terminated by a semicolon. A declaration of a class is akin to the declaration of a function. It tells the compiler about the class and its properties. Declaration of a class alone does not make a difference to the execution of a program, as the class needs to be used just the same way as a function needs to be invoked. A class that models a human looks like the following (ignore syntactic short-comings for the moment): class Human { // Data attributes: string Name; string DateOfBirth; string PlaceOfBirth; string Gender; // Methods: void Talk(string TextToTalk); void IntroduceSelf(); … }; Needless to say, IntroduceSelf() uses Talk() and some of the data attributes that are grouped within class Human . Thus, in keyword class, C++ has provided you with a powerful way to create your own data type that allows you to encapsulate attributes and functions that work using those. All attributes of a class, in this case Name, DateOfBirth , PlaceOfBirth , and Gender , and all functions declared within it, namely Talk() and IntroduceSelf() , are called members of class Human. Encapsulation, which is the ability to logically group data and methods that work using it, is a very important property of Object Oriented Programming. Instantiating an Object of a Class Declaring a class alone has no effect on the execution of a program. The real-world avatar of a class at runtime is an object. To use the features of a class, you typically instantiate an object of that class and use that object to access its member methods and attributes. Instantiating an object of type class Human is similar to creating an instance of another type, say double double Pi = 3.1415; // a double declared as a local variable (on stack) Human Tom; // An object of class Human declared as a local variable Alternatively, you would dynamically allocate for an instance of class Human as you would an int using new: int* pNumber = new int; // an integer allocated dynamically on free store delete pNumber; // de-allocating the memory Human* pAnotherHuman = new Human(); // dynamically allocated Human delete pAnotherHuman; // de-allocating memory allocated for a Human
المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .
|