التخطي إلى المحتوى الرئيسي

شرح ع كلاس class للغة c++

شرح ع كلاس class للغة c++
اسئله عن موضوع  كلاس class للغة c++

Lab 4: Using different handles to access class members,and Writing and using destructors.

 

Lab Objectives:

ü Understand class scope and accessing class members via the name of an object, a reference to an object or a pointer to an object.

ü Writing and using destructors to perform "termination housekeeping" on an object before it is destroyed.

ü Declaring local, global, and static local objects.

ü Learn when constructors and destructors are called and the order in which they are called.

ü Defining utility functions.

Student Learning Outcomes:

You will:

ü Be able to use object name or a reference to an object or a pointer to an object to access different class members.

ü Be able to declare destructor for any class to perform termination housekeeping.

ü Be able to identify the order in which the constructors and destructors are called for a set of objects (which have different storage classes (automatic or static) and different scopes (local and global)).

ü Be able to write utility function and use it.

Background:

You have learned the basic concept of using handlings to access class members. You have also learned how to write a destructor for a given class.

You have also known what the utility function is.

Assumption:

Read the summary in page#14from the CS118 lab Manual that explains the Order of constructor, destructor function calls for global, static, and local objects.

LIST OF Exercises:-

Exercise #1:

Write a full documented and separated interface C++ program that implements a class called Student, giving the specification below:

 

Data that is associated with a Student class:

o An array to hold the marks of the student for five courses.

o Name: of type string to hold student name.

 

Functions that can be performed on Student class:

o Parameterized constructor to initialize object data members to any given values.

o Destructor: that just prints the message “Class Student Destructor”.

o SetAll function: to assign new values to the data members.

o Utility function to calculate the summation of student marks.

o Access function called Average: to calculate the average of the student marks.

o Access function called Print: to print the information of the student.

 

Write a driver program that test class Student as follows:

o Define an object of type student named s1, a pointer to s1 named ptr1, and a reference to s1 named ref1.

o Change the values of s1 by calling the function setAll using the reference ref1.

o Compute the average of s1 using the pointer ptr1.

o Print the information of s1.

 

 

Exercise #2:

 

Write a class implementation for a class named My_Class giving the specification below:

 

Data that is associated with this class are:

o ID: of type integer.

o Msg: of type string, to hold a simple description of any created object (local, global, static, or automatic).

 

Member Functions:

o Constructor with 2 arguments to:

§ Initialize object data members ID and Msg with any given value, and print the following statement

cout<<”constructor ”<<ID<<”\t”<<Msg<<endl;

o Destructor to:

§ print the ID and Msg of any terminated object as follow:

cout<<”Destructor ”<<ID<<”\t”<<Msg<<endl;

 

Also you have to write (outside the class):

o A global void function named Create_Fun( ) that declares 2 objects:

obj1: local automatic to Create_Fun ( ).

obj2: static local to Create_Fun ( ).

Hint: Use the following code:

void create_Fun()

{

cout<<"\n create Function:Excution Begin"<<endl;

my_class obj1(1,"Local to Create fun ");

static my_class obj2(2,"Static Local to Create fun ");

cout<<"\n create Function:Excution END"<<endl;

}

 

Write a driver program that test the class as follow:

1) Declare a global object named obj3.[id=3 , msg=”Global]

2) Declare a local object to the main function named obj4.

[id=4 , msg=”local to main”]

3) Declare a static local object to the main function named obj5.

[id=5 , msg=”static local to main”]

4) Call function Create_Fun ( ) from the main function.

5) Declare a local object named obj6 to the main function.

[id=6, msg="local to main”]

6) Make another Call for function Create_Fun ( ) from the main function.

7) Declare a local object named obj7 to the main function.

[id=7, msg="local to main”]

تعليقات

  1. مع شركة عزل خزانات المياه ستضمن عملية عزل بطريقة علمية وصحية مطابقة للمواصفات العالمية وسنعطيك ضمان شامل فى حالة وجود اي مشكلات قد تطرأ بعد الانتهاء من العزل
    عزل خزانات بالمدينة المنورة
    فشركتنا تعد من أفضل شركات عزل الخزانات وسر براعتها فى هذا المجال هو إعتماد عزل خزانات بالمدينه المنوره على فريق عمل متخصص فى كيفية إستخدام الطرق والأدوات الحديثة لعزل الخزانات والعمل على طهارتها من أي شوائب توجد بها،واهم ما يميز
    عزل خزانات بالمدينه المنوره
    عن شركات عزل الخزانات بالمدينة المنورة فإنها تقدم لك أفضل الخدمات وبأسعار مناسبة جدا .فإذا كان لديك خزانا تجد صعوبة بالغة فى تنظيفه فإليك عزل خزانات المياه سوف تقوم بمساعدتك على هذه المشكلة وفى أسرع وقت ممكن وذلك بتقديم أفضل الحلول
    شركة عزل خزانات بالمدينة المنورة
    وفقا لمعايير دقيقة لديها لضمان إتمام عملية العزل بنجاح.

    ردحذف

إرسال تعليق

المشاركات الشائعة من هذه المدونة

شرح + اسئله لموضوع Pointers في لغة c++

شرح + اسئله لموضوع Pointers في لغة c++ Lab 2: Using Pointers Lab Objectives: In this lab students will learn: ü Memory concept of variables, pointers and how to use variable identifiers and pointers to refer to the variable. ü Pointer variable declarations and initialization. ü Direct and indirect referencing a variable using the pointer operators. ü Using * and address (&) operators.   Background: When declaring a variable, it is located at a specific location in memory, the memory address. The task of locating variables is automatically performed by the operating system during runtime. In some cases we need to know the address where the variable is being stored during runtime. Variable which stores a reference to another variable is called a pointer. We can directly access the value stored in the variable using a ...

شرح + اسئله عن موضوع Arrays في لغة c++

شرح + اسئله عن موضوع  Arrays في لغة c++  1: Arrays   Lab Objectives: ü Learn how to use the array data structure to represent a set of related data items. ü Learn how to declare arrays, initialize arrays and refer to the individual elements of arrays. ü Learn how to pass arrays to functions. ü Learn how to declare and manipulate Two-dimensional arrays. ü  Background:   Definition Array: A collection of individual values, all of the same data type, stored in adjacent memory locations. One Dimensional Array: An array with a single variable index. Using the array name together with an integral valued index in square brackets refers to the individual values. The first array element always has the subscript 0. The second array element has the subscript 1, etc. The base address of an array ...

شرح واسئله عن Friend function & friend class.

موضوع Friend function & friend class Task #6: Friend function & friend class.   Create and destroy objects dynamically. Static data members and member functions.     Objectives:   Using friend functions, and friend classes.   Using this pointer.    Creating and destroying objects dynamically with operators new and delete, respectively.    Creating dynamic array class.   Using static data members and member functions.   Student Learning Outcomes:   You will: Be able to write a friend function and friend class. Be able to allocate and de allocate objects dynamically.   Background:   You have learned the syntax of friend function, friend class, this pointer, and delete and new operators. You have also learned how to write a static data members and member functions.