Axborot xavfsizligini boshqarish yo`nalaishi 1-kurs talabalari uchun uchun «Dasturlash II» fanidan laboratoriya mashg`uloti
2-laboratoriya mashg‘uloti. Tiplarni almashtirish operatorlari
Masala
1. Masalaning berilishi: Ikkita butun sonni bir-biriga bo`lib haqiqiy sonni natijaga chiqarimg.
Parent nomli klass yarating.
Undan 2 ta voris klass oling.
Dynamic_case orqali voris klass va ota klass pointerlarni bog`lang hamda tekshiring.
2. Masalani yechish g‘oyasi
#include
using namespace std;
class Parent
{
virtual void print()
{
cout << "Derived" << endl;
}
};
class Boy: public Parent
{
void print()
{
cout << "Derived1" << endl;
}
};
class Girl: public Parent
{
void print()
{
cout << "Derived2" << endl;
}
};
int main()
{
int a=56, b=9;
float c = (float)a/b;
cout << c << endl;
Boy A;
Parent* B = dynamic_cast
(&A);
Girl* C = dynamic_cast(B);
if (C == nullptr)
cout << "null" << endl;
return 0;
}
3. Dastur kodi.
Do'stlaringiz bilan baham: |