Curs 3 Flashcards
class MyClass
{
int x;
public:
MyClass(int value) { this->x = value; }
};
class Date
{
MyClass m;
public:
Date() { }
};
void main()
{
Date d;
}
slide 25
error C2512: ‘MyClass’: no appropriate default
constructor available
class MyClass
{
int x;
public:
MyClass(int value) { this->x = value; }
};
class Date
{
MyClass m;
public:
Date() : m(123) { }
};
void main()
{
Date d;
}
slide 26
This code will compile properly
class MyClass
{
int x;
public:
MyClass(int value) { this->x = value; }
};
class Date
{
MyClass m;
public:
Date() : m(123) { }
Date(int value) { }
};
void main()
{
Date d;
}
slide 27
error C2512: ‘MyClass’: no appropriate default
constructor available
This code will NOT compile. There is at least one constructor that does not
instantiate data member “m” from class Date.
class MyClass
{
int x;
public:
MyClass(int value) { this->x = value; }
};
class Date
{
MyClass m;
public:
Date() : m(123) { }
Date(int value) : m(value+10) { }
};
void main()
{
Date d;
}
the code compiles correctly
class MyClass
{
int x;
public:
MyClass(int value) { this->x = value; }
MyClass() { this->x = 0; }
};
class Date
{
MyClass m;
public:
Date() { }
Date(int value) : m(value+10) { }
};
slide 29
This code compiles correctly.
class MyClass
{
int x;
public:
};
class Date
{
MyClass m;
public:
Date() { }
Date(int value) { }
};
slide 30
This code compiles correctly
class MyClass
{
int x;
public:
MyClass(int value) { this->x = value; }
MyClass() { this->x = 0; }
};
class Date
{
MyClass m = { 123 };
public:
Date() { }
Date(int value) { }
};
slide 31
yes
class MyClass
{
int x;
public:
MyClass(int value) { this->x = value; }
MyClass() { this->x = 0; }
};
class Date
{
MyClass m = 123;
public:
Date() { }
Date(int value) { }
};
slide 31
yes
class Tree {
public:
Tree(const char * name) { printf(“Tree: %s\n”, name); }
};
class Car {
public:
Car(const char * name) { printf(“Car: %s\n”, name); }
};
class Animal {
public:
Animal(const char * name) { printf(“Animal: %s\n”, name); }
};
class Object
{
Tree t;
Car c;
Animal a;
public:
};
void main() {
Object obj;
}
slide 32
This code will not compile because data members obj.t , obj.c and obj.a need a
custom call to their own constructor.
class Tree {
public:
Tree(const char * name) { printf(“Tree: %s\n”, name); }
};
class Car {
public:
Car(const char * name) { printf(“Car: %s\n”, name); }
};
class Animal {
public:
Animal(const char * name) { printf(“Animal: %s\n”, name); }
};
class Object
{
Tree t;
Car c;
Animal a;
public:
Object(): t(“oak”), a(“fox”), c(“Toyota”) {}
};
void main() {
Object obj;
}
slide 33
Output:
Tree: oak
Car: Toyota
Animal: fox
class Tree {
public:
Tree() { printf(“CTOR: Tree\n”); }
};
class Car {
public:
Car() { printf(“CTOR: Car\n”); }
};
class Animal {
public:
Animal() { printf(“CTOR: Animal\n”); }
};
class Object
{
Tree t1,t2;
Car c;
Animal a;
};
void main() {
Object obj;
}
slide 34
Output:
CTOR: Tree
CTOR: Tree
CTOR: Car
CTOR: Animal
class Object
{
int x, y, z;
public:
Object(int value) : x(value), y(xx), z(valuey) {}
};
void main()
{
Object o(10);
}
slide 35
o.x = 10 (the first one to be computed)
❑ o.y = o.x * o.x = 10 * 10 = 100 (the second one to be computed)
❑ o.z = 10 * o.y = 10 * 100 = 1000 (the third one to be computed)
class Object
{
int x, y, z;
public:
Object(int value) : y(value), z(value/2), x(y*z) {}
};
void main()
{
Object o(10);
}
slide 36
o.x = o.y * o.z = unknown results (it depends on the values that resides on the stack when
the instance “o” is created). (the first one to be computed !)
❑ o.y = 10 (the second one to be computed)
❑ o.z = 10 (value) / 2= 10 / 2 = 5 (the third one to be computed)
class Date
{
private:
int x;
const int y;
public:
};
void main()
{
Date d;
}
slide 38
const int y nu e initializat
class Date
{
private:
int x;
int & y;
public:
};
void main()
{
Date d;
}
slide 38
int & y nu e initializat
class Date
{
private:
int x;
const int y;
public:
Date();
};
Date::Date() : x(100), y(123)
{
}
void main()
{
Date d;
}
slide 40
The code compiles – y is initialized with value 123 in example
class Date
{
private:
int x;
int & y;
public:
Date();
};
Date::Date() : x(100), y(x)
{
}
void main()
{
Date d;
}
slide 40
yes
class Date
{
private:
int x;
const int y;
public:
Date();
Date(int value);
};
Date::Date() : x(100), y(123)
{
}
Date::Date(int value) : x(value)
{
}
slide 41
no
class Date
{
private:
int x;
int & y;
public:
Date();
Date(int value);
};
Date::Date() : x(100), y(x)
{
}
Date::Date(int value) : x(value)
{
}
slide 41
no
class Date
{
private:
int x;
const int y;
public:
Date();
Date(int value);
};
Date::Date() : x(100), y(123)
{
}
Date::Date(int value) : x(value), y(value*value)
{
}
void main()
{
Date d;
D
slide 42
yes
class Date
{
private:
int x;
const int y;
public:
Date();
};
Date::Date() : x(100)
{
y = 123;
}
void main()
{
Date d;
}
slide 43
no
class Date
{
private:
int x;
int & y;
public:
Date();
};
Date::Date() : x(100)
{
y = x;
}
void main()
{
Date d;
}
slide 43
no