Compatibility of Base Classes and Derived Classes


GV? Do you have any comments on the commands in the above program?

Comment: Constructor A( int xx ) ; There is no effect in the above program.


Maybe you are interested!

- To use a constructor with base class parameters, we must declare it in the derived class constructor as follows:

Derived_constructor (argument_list): base_constructor (argument_list)

Compatibility of Base Classes and Derived Classes

{ declaration for derived constructor ; }

For example: Rewrite the above program:

class A

{ protected :

int x ; public :

A( ) { x = 5 ; }

A( int xx ) { x = xx ; }

void print( ) { cout << “x= ” << x ; }

} ;

GV? Rewrite the above program and show the results that appear on the screen after running the program.


class B : public A

{ protected :

int y ; public :

B( ) { y = 7 ; }

B( int yy ) { y = yy ; }

B( int xx , int yy ): A( xx ) { y = yy ; }

void print( ) { A :: print( ) ;

cout << “y= ” << y ; }

} ;

void main( )

{ B O1 , O2( 10 ) , O3 ( 1, 9 ) ;

O1. print( ) ;

O2. print( ) ;

O3. print( ) ;

}


The teacher waits for students to finish writing the results of the program and then analyzes and checks the results.


The result appears on the screen: x = 5 y = 7

x = 5 y = 10

x = 1 y = 9


2.7 Compatibility of base class and derived class

a. Compatibility of object classes:

- Objects of a derived class can be assigned to objects of the base class.

For example, Let A be the base class, B be the class derived from class A. We can perform the following assignment:

A x ; By ; x = y ;

In the above assignment, note:

+ x can only use components of the base class, and is not allowed to access additional or modified components of the derived class.

+ do not assign base class objects to derived classes, i.e. y = x ; is wrong, cannot be done.


b. Object pointer compatibility:

- The base class pointer can contain the address of the derived class object.

For example:

A*pa ; By ; pa = &y ;

pa enter( ) ; // import( ) of base class A

Example illustrating the above compatibility:


class A

{ protected :

int x ; public :

A( ) { x = 5 ; }

A( int xx ) { x = xx ; }

void Print( ) { cout << “x= ” << x ; }

} ;

class B : public A

{ protected :

int y ; public :

B( ) { y = 7 ; }

B( int xx , int yy ): A( xx ) { y = yy ; }

void Print( ) { A :: print( ) ;

cout << “y= ” << y ; } void Tong( ) { A :: print( ) ;

cout << “Total = ” << x + y ; }

} ;

void main( )

{ A O1( 1 ) , *PO1 ;

B O2( 2, 3 ) ;

O1 = O2 ; // (1)

O1. Print( ) ; // x = 2, explained through illustration 2.9 below O1. Tong( ) ; // wrong because O1 does not have a method Tong( );

The assignment O1 = O2 will not allow access to the additional member (Tong( )) of derived class B see explanatory illustration 2.10 below

PO1 = &O2 ; // (2)

PO1 Print( ) ; // x = 2

PO1 Tong( ) ;// is wrong because it does not access the Tong( );

}


Teacher: Analyze more clearly in the following illustration.


O1 = O2

O1

O2

O1

O2

Figure 2.9. Explain access after performing assignment (1) (O1 = O2;)

In the example above illustrated.

PO1

O2

Figure 2.10. Explain the access after performing assignment (2) (PO1 = &O2;) in the example above.


x = 1 Print( )


x = 2

y = 3 Print( ) Tong( )


x = 2 Print( )

x = 2 Print( )

y = 3 Tong( )

x = 2

A :: Print( )

y = 3 Print( ) Tong( )

Activity 5 : Instructions for learning at home

Exercise 1: For the purpose of calculating the area of ​​squares, rectangles, circles, trapezoids, and triangles.

Let's organize the classes so that the implementation data element can be inherited:

- Structure

- Import, export

- Calculate area.


Exercise 2: The library has 3 types of materials: books, magazines, and textbooks. There is general data: document code, location, quantity, unit price. And have separate data as follows:

- Book: author, title, year of publication.

- Magazine: magazine name, specialty.

- Textbook: author, name.

Organize classes so that they inherit common data. Proceed with installation: - Construct, destroy

- Import and export procedures.


APPENDIX 4 – ILLUSTRATIVE TEACHER 2 Lesson “EXERCISE ON SINGLE INHERITANCE”

(3 periods in class)


A. LESSON OBJECTIVES

1. Knowledge – Skills:

- Apply learned knowledge to write programs with legacy applications to solve a few simple problems associated with practice.

- Detect and handle syntax errors during program writing.

2. Develop the expression of computational thinking

- Develop the ability to analyze problems, analyze each specific operation of syntax into component operations to clearly understand the operation of any syntax. As well as develop the ability to draw operational analysis diagrams.

- Develop pattern recognition ability to select methods that need to be declared in each class to use inheritance in object-oriented programming.

- Develop the ability to analyze and design algorithms to build programs to perform required tasks.

B. PREPARATION WORK

1. Teachers prepare:

- Research lesson content: Research the Programming Engineering Syllabus (for KTĐT, VT students), related reference documents, prepare teaching plans, lesson plans in the direction of organizing learning activities for students. SV.

- Develop teaching plans and lesson plans in the direction of organizing learning activities for students with a system of questions to suggest learning activities and a system of exercises to practice skills in lessons.


2. Students prepare:

- Review the knowledge learned in previous lessons related to the lesson content.

3. Teaching aids:

- Programming techniques textbooks, reference materials.

- Computer, projector

- Lectures (electronic)

- Programming tools (C/C++)

C. TEACHING METHODS

- Teaching method: Collaborative teaching, "puzzle piece" technique.

D. ACTIVITIES CONTENT

- Practice writing programs that use single inheritance in the program.

Exercise 1: For the purpose of calculating the area of ​​squares, rectangles, circles, trapezoids, and triangles.

Let's organize the classes so that the implementation data element can be inherited:

- Structure

- Import, export

- Calculate area.

Exercise 2: The library has 3 types of materials: books, magazines, and textbooks. There is general data: document code, location, quantity, unit price. And have separate data as follows:

- Book: author, title, year of publication.

- Magazine: magazine name, specialty.

- Textbook: author, book title.

Organize classes so that they inherit common data. Proceed with installation: - Construct, destroy

- Import and export procedures.


E. SUGGESTIONS FOR TEACHING PROCESS Activity 1: Practice

* Operational objectives:

- Create opportunities for students to apply and increase their proficiency in using single inheritance in object-oriented programming.

- Create learning excitement for students through math problems closely related to practice.

* Student learning products: Execution program segment.

* How teachers and students carry out activities:

- The "puzzle piece" technique often takes a lot of time to organize and create groups.

- The "puzzle piece" technique requires that the tasks assigned to each group of experts must be equivalent in workload and especially must have high independence. “High independence” means no job has to be done after another job. Therefore, the two exercises mentioned in the activity are independent of each other and have equal difficulty.

- The "puzzle piece" technique requires teachers to pay close attention to the ability to observe and cover the class to ensure regulation of students' learning activities. In general, the exercises outlined in the activity are not difficult because they only require direct application. However, for classes with low cognitive ability, teachers will regulate as follows:

+ In step 2, if time is running out, for each assigned task, the teacher only needs one group of experts to do it correctly and when that group is done, they immediately ask students to move to step 3.

+ In step 3, if the "product transfer" activity is not effective, the teacher tells the groups to stop and switch to the teaching method.

Comment


Agree Privacy Policy *