ООП (5 семестр) / Контрольная
.doc
-
Что выведет следующий код при создании экземпляра класса X:
#іnclude <iоstrеam>
using namesрace std;
clаss Y {
publіc:
Y() { сout << "Y"; }
};
clаss Z {
publiс:
Z() { соut << "Z"; }
};
class X : рublіс Z {
privatе:
Y m_objY;
publiс:
Х() { сout << "Х"; }
};
-
XY
-
YX
-
XYZ
-
YXZ
-
ZYX
-
Что выведет следующий код:
class Base
{
public:
void Method ()
{
cout << "Базовый класс\n";
}
};
class Derived : public Base
{};
// внутри main
Base b;
Derived d;
b.Method();
d.Method();
-
Что выведет следующий код:
class Base
{
public:
virtual void Method ()
{
cout << "Базовый класс\n";
}
};
class Derived : public Base
{
void Method ()
{
cout << "Производный класс\n";
}
};
// внутри main
Base* b = new Derived;
Derived* d = new Derived;
b->Method();
d->Method();
-
Перегрузка или переопределение метода:
class Rectangle
{
public:
void print() const;
private:
double length;
double width;
};
void Rectangle::print() // метод print() базового класса
{
cout << "Length = " << length << "; Width = " << width;
}
class Box : public Rectangle
{
public:
void print() const;
private:
double height;
};
void Box::print() // метод print() производного класса
{
Rectangle::print(); // вызов родительского метода print()
cout << "; Height= " << height;
}
-
Создать массив студентов в статической и динамической памяти и заполнить с помощью функции void Set (char*,int, float)
const int LNAME=25;
class STUDENT
{
char name[LNAME];
int age;
float grade;
public:
STUDENT();
STUDENT(char*,int,float);
~STUDENT();
char * GetName() ;
int GetAge() const;
float GetGrade() const;
void SetName(char*);
void SetAge(int);
void SetGrade(float);
void Set(char*,int,float); // фамилия, возраст, средний балл
void Show();
};
// внутри main
-
Что выведет следующий код
#include <iostream.h>
class A {
public:
A()
{
cout<<"Constructor A\n";
}
void FriendToB();
void SomeFunction();
};
class B
{
private:
int i,j;
public:
B()
{
i=10;
j=17;
cout<<"Constructor B\n";
}
friend void A::FriendToB();
};
class C
{
private:
int k,n;
public:
C()
{
k=4;
n=7;
cout<<"\n\nConstructor C\n";
}
friend class A;
};
void A::FriendToB()
{
B example;
cout<<"Значения переменных:\n";
cout<<"example.i="<<example.i;
cout<<"\nexample.j="<<example.j<<endl;
}
void A::SomeFunction()
{
C temp;
cout<<"Тест дружественного класса...\n";
cout<<"temp.k="<<temp.k;
cout<<"\ntemp.n="<<temp.n<<endl;
}
void main()
{
A test;
test.FriendToB();
test.SomeFunction();
}