seteer und Getter conflict with default constructor - constructor

i write a c++ Code contain two clases and one derived class from both simultaneouously
in main funktion i have an object from the derived class declared with no constructor because i have already declared a default constructor in the base classes and those will be inhereted in the derived class without problem, then a tried to modify the member variable through setter functions that i have declared in the base clases this also should from my point of wiew implemented without problem. the problem is when i try to execute the Programm it will be excuted as i dont declare setter functions the default constructor hier has the priority and will be excute again what should i do i want modify the memeber variables through setter

Related

The compiler generated default constructor is not used if we create a user-defined default constructor, this process is called as..?

The compiler generated default constructor is not used if we create a user defined default constructor, this process is called as..? Like how we have constructor overloading, function overriding.. does cpp name this one.
is it overwriting the compiler generated constructor

Must class constructor variables be assigned to private class variables or is there a less code intensive way?

When creating a class you have to assign its parameters to private class variables in order to use those private variables in other procedures and functions in the class. This is code intensive since you have to define the same variable twice.
I have tried using a constructor parameter in a procedure of the same class but it did not work without having to
type
WorldObject=class
private
privX, privY, privStartSpriteNum, privEndSpriteNum:word;
privDirection, privTurn:byte;
public
constructor create(x, y, startSpriteNum, endSpriteNum:word; direction, turn:byte);
procedure draw;
constructor WorldObject.create(x, y, startSpriteNum, endSpriteNum:word; direction, turn:byte);
begin
privX:=x;
privY:=y;
privStartSpriteNum:=startSpriteNum;
privEndSpriteNum:=endSpriteNum;
privDirection:=direction;
privTurn:=turn;
end;
procedure WorldObject.draw;
begin
writeLn(privX);
writeLn(WorldObject.x);
end;
Was expecting writeLn(WorldObject.x) to print the value of x.
But got the error: Identifier idents no member "x".
Is there not a way to use the parameter variables from the constructor without having to pass them to private variables?
Thanks!
You seem to be hoping that there is a mechanism where parameters to constructors are automatically stored somewhere, for retrieval later. There is no such mechanism. It is your responsibility to store any state that you need to refer to outside the scope of your constructor. Using fields of your class is the classic method to do this.
You can write properties instead of copying private variables, you just need to specify where to read and where to write, since then you can use properties in nested classes or even outside the class.
If you need to use them in the nested classes, declare as protected, if not - as public.
property MyProp: TYPE read WriteMyProp read GetMyProp; // for procedure to write and function to read;
property MyPropV: TYPE read MyPropValue write MyPropValue; // for deriving from variable;

Why does Kotlin have two types of constructors?

Kotlin has two types of constructors, primary and secondary. What is the purpose of having two types? In my opinion it makes the code more complicated and inconsistent. If both types of constructors create objects of a class, they are equally important to a class.
Meanwhile, multiple initialisers also introduce confusion and reduce readability.
Primary constructors cover the poplular use case when you need to save the values passed as the constructor arguments to the properties of the instance.
Basically, a primary constructor provides a shorthand for both declaring a property and initializing it from the constructor parameter.
Note that you can do the same without primary constructors at all:
class Foo {
val bar: Bar
constructor(barValue: Bar) {
bar = barValue
}
}
But, since this happens really often in the codebases, Kotlin primary constructors serve the purpose of reducing the boilerplate here:
class Foo(val bar: Bar)
Secondary constructors may complement or replace the primary one in order to support several construction routines for a single class.
Philosophy: the main purpose - kotlin is pragmatic language. The main idea of it: exclude the most frequent boilerplate.
A lot of classes, which are used in the C#/Java have only one constructor with the following semantic:
Part of parameters are stored into the fields (with the same names)
Part of parameters are used by constructor to create other field (or extra validation)
Moreover, a lot of secondary constructors are used to call the primary constructor with default values (please see this question for C# language)
Therefore: to have simplified code (which reflects essence) you have to have ability to:
Support constructor parameters, which will be stored into the fields automatically (without this.myData = myData)
Support ability to create field from constructor parameters
Both of items above required, therefore all constructors have the same input values (because all fields should be initialized, however they are set out of constructor body). Therefore you have to have primary constructor, which will do initialization.
After this logic applying we get major rule: to cover the most frequent class initializing scenarios you have to have primary constructor with ability to define default parameter values. Additionally you should have ability to create secondary constructors to cover all other scenarios.
So, I repeat the main idea: primary constructor is needed to cover the most frequent cases (pragmatic purpose)
Primary constructor can define what parameters are passed into the init blocks. For example:
class Foo(a: Bar){
val b : Bar
init {
b = a // value of "a" is known from primary constructor
}
constructor(a: Boo) : this(a.toBar())
}
Without explicit primary constructor call it would be impossible to determine what value/type of a should be used in init.
Primary constructor and initializer blocks always execute before secondary constructor block (doc).

AS3 constructor has no method with the same name as the class

I was reading ActionScript 3.0 Abstract Factory Design Pattern: Multiple Products and Factories, and I have the following,
private var busFactory:AbFactory;
busFactory=new BusinessFactory();
Both in BusinessFactory.as and in AbFactory.as there is no method with the same name as the class, only createProductA and createProductB. So, how could busFactory call the constructor with new BusinessFactory ?
The default constructors for both AbFactory and BusinessFactory (which the article shows inherits from AbFactory) are created by the compiler.
If you start to pass arguments to the BusinessFactory() constructor, the compiler will complain about an unexpected argument count. That's when you need to write the constructor yourself. But passing nothing means the default can be used.
Also the reason that you could set a AbFactory to a BusinessFactory means that they share the same heritage.

When subclassing in AS3, is a new constructor function necessary?

Basic OOP question...
I want to add a couple functions to the Array class so that my program will be amazing and make me rich and famous overnight.
So I create a new subclass NewArray which extends Array. Do I need to write a constructor method for NewArray? If I leave it blank will it just use the parent's (Array's) constructor method?
Thanks
Yes, if you leave it blank it'll use the super-class' default constructor, which in the case of an Array is actually a constructor with a default value set:
Array(numElements:int = 0)
So by default you'll be creating an array with zero elements, which I guess is probably what you want anyway.
And don't forget this note from the docs:
You can extend the Array class and override or add methods. However, you must specify the subclass as dynamic or you will lose the ability to store data in an array.
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Array.html