Part 10

2511 days ago, 954 views
PowerPoint PPT Presentation
Be that as it may, we need the year, so we now indicate the year individual from the birthday part ... birthday.output( ); Note that today and birthday have their own particular ...

Presentation Transcript

Slide 1

Section 10 Defining Classes

Slide 2

Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types

Slide 3

10.1 Structures

Slide 4

What Is a Class? A class is an information sort whose factors are protests Some pre-characterized classes you have utilized are int burn ifstream You can characterize your own classes too

Slide 5

Class Definitions A class definition incorporates A depiction of the sorts of qualities the variable can hold A portrayal of the part capacities We will begin by characterizing structures as an initial move toward characterizing classes

Slide 6

Structures A structure can be seen as a question Contains no part capacities (The structures utilized here have no part capacities) Contains various estimations of conceivably extraordinary sorts The different qualities are legitimately related as a solitary thing Example: A bank Certificate of Deposit (CD) has the accompanying qualities: a adjust an loan cost a term (months to development)

Slide 7

Remember this semicolon! The CD Definition The Certificate of Deposit structure can be characterized as struct CDAccount { double adjust; double interest_rate; int term;/months to development }; Keyword struct starts a structure definition CDAccount is the structure tag or the structure's sort Member names are identifiers pronounced in the supports

Slide 8

Using the Structure definition is for the most part set outside any capacity definition This makes the structure sort accessible to all code that takes after the structure definition To announce two factors of sort CDAccount : CDAccount my_account , your_account ; My_account and your_account contain unmistakable part factors adjust, interest_rate , and term

Slide 9

The Structure Value The Structure Value Consists of the estimations of the part factors The estimation of a protest of sort CDAccount Consists of the estimations of the part factors balance interest_rate term

Slide 10

Specifying Member Variables Member factors are particular to the structure variable in which they are proclaimed Syntax to determine a part factor: Structure_Variable_Name . Member_Variable_Name Given the revelation: CDAccount my_account , your_account ; Use the speck administrator to determine a part factor my_account.balance my_account.interest_rate my_account.term

Slide 11

Using Member Variables Member factors can be utilized pretty much as some other variable of a similar sort my_account.balance = 1000; your_account.balance = 2500; Notice that my_account.balance and your_account.balance are distinctive factors! my_account.balance = my_account.balance + intrigue;

Slide 15

Duplicate Names Member variable names copied between structure sorts are not an issue. super_grow.quantity and apples.quantity are diverse factors put away in various areas struct FertilizerStock { twofold amount ; twofold nitrogen_content; }; FertilizerStock super_grow; struct CropYield { int amount ; twofold size; }; CropYield apples;

Slide 16

Structures as Arguments Structures can be contentions in capacity calls The formal parameter can be call-by-esteem The formal parameter can be call-by-reference Example: void get_data ( CDAccount & the_account ); Uses the structure sort CDAccount we saw before as the sort for a call-by-reference parameter

Slide 17

Structures as Return Types Structures can be the sort of an esteem returned by a capacity Example: CDAccount shrink_wrap (twofold the_balance , twofold the_rate , int the_term ) { CDAccount temp; temp.balance = the_balance ; temp.interest_rate = the_rate ; temp.term = the_term ; return temp; }

Slide 18

Using Function shrink_wrap fabricates a total structure esteem in temp, which is returned by the capacity We can utilize shrink_wrap to give a variable of sort CDAccount an esteem along these lines: CDAccount new_account ; new_account = shrink_wrap (1000.00, 5.1, 11);

Slide 19

Assignment and Structures The task administrator can be utilized to allocate qualities to structure sorts Using the CDAccount structure again: CDAccount my_account, your_account; my_account.balance = 1000.00; my_account.interest_rate = 5.1; my_account.term = 12; your_account = my_account; Assigns all part factors in your_account the relating values in my_account

Slide 20

Hierarchical Structures can contain part factors that are likewise structures struct PersonInfo contains a Date structure struct PersonInfo { twofold stature; int weight; Date birthday ; }; struct Date { int month; int day; int year; };

Slide 21

Using PersonInfo A variable of sort PersonInfo is proclaimed by PersonInfo person1; To show the birth year of person1, first get to the birthday individual from person1 cout << person1.birthday … But we need the year, so we now determine the year individual from the birthday part cout << person1.birthday.year;

Slide 22

Initializing Classes A structure can be instated when pronounced Example: struct Date { int month; int day; int year; }; Can be introduced thusly Date due_date = {12, 31, 2004};

Slide 23

10.2 Classes

Slide 24

Classes A class is an information sort whose factors are articles The meaning of a class incorporates Description of the sorts of estimations of the part factors Description of the part capacities A class portrayal is to some degree like a structure definition in addition to the part capacities

Slide 25

A Class Example To make another sort named DayOfYear as a class definition Decide on the qualities to speak to This illustration's qualities are dates, for example, July 4 utilizing a whole number for the quantity of the month Member variable month is an int (Jan = 1, Feb = 2, and so on.) Member variable day is an int Decide on the part capacities required We utilize only one part work named output

Slide 26

Class DayOfYear Definition class DayOfYear { public: void yield( ); int month; int day; }; Member Function Declaration

Slide 27

Defining a Member Function Member capacities are announced in the class revelation Member work definitions recognize the class in which the capacity is a part void DayOfYear ::yield() { cout << "month = " << month << ", day = " << day << endl ; }

Slide 28

Member Function Definition Member work definition language structure: Returned_Type Class_Name::Function_Name(Parameter_List) { Function Body Statements } Example: void DayOfYear::output( ) { cout << "month = " << month << ", day = " << day << endl; }

Slide 29

The "::" Operator "::" is the extension determination administrator Tells the class a part capacity is an individual from which class void DayOfYear ::yield( ) demonstrates that capacity yield is an individual from the DayOfYear class The class name that goes before "::" is a sort qualifier

Slide 30

"::" and "." "::" utilized with classes to distinguish a part (definition) void DayOfYear ::yield( ) {/work body } '.'utilized with factors to distinguish a part (call/reference) DayOfYear birthday; birthday.output ( );

Slide 31

Calling Member Functions Calling the DayOfYear part work yield is done thusly: DayOfYear today, birthday; today.output ( ); birthday.output ( ); Note that today and birthday have their own adaptations of the month and day factors for use by the yield work

Slide 34

Encapsulation is Combining various things, for example, factors and capacities, into a solitary bundle, for example, a protest of a class

Slide 35

Problems With DayOfYear Changing how the month is put away in the class DayOfYear obliges changes to the program If we choose to store the month as three characters (JAN, FEB, and so forth.) rather than an int cin >> today.month will no longer work since we now have three character factors to peruse if(today.month == birthday.month) will no longer work to think about months The part work "yield" does not work anymore

Slide 36

Ideal Class Definitions Changing the execution of DayOfYear obliges changes to the program that utilizations DayOfYear A perfect class meaning of DayOfYear could be changed without obliging changes to the program that utilizations DayOfYear

Slide 37

Fixing DayOfYear To settle DayOfYear We have to add part capacities to utilize while changing or getting to the part factors If the program never specifically references the part factors, changing how the factors are put away won't require changing the program We should make certain that the program does not ever straightforwardly reference the part factors

Slide 38

Public Or Private? C++ helps us confine the program from straightforwardly referencing part factors private individuals from a class must be referenced inside the meanings of part capacities If the program tries to get to a private part, the compiler gives a blunder message Private individuals can be factors or capacities

Slide 39

Private Variables Private factors can't be gotten to specifically by the program Changing their qualities requires the utilization of open part elements of the class To set the private month and day factors in another DayOfYear class utilize a part capacity, for example, void DayOfYear ::set( int new_month , int new_day ) { month = new_month ; day = new_day ; }

Slide 40

Public or Private Members The watchword private recognizes the individuals from a class that can be gotten to just by part elements of the class Members that take after the catchphrase private will be private individuals from the class T