Hi, I have a question regarding classes and constructors - oop

class Stack {
private:
int stackstore[100];
int SP;
public:
Stack(void) { SP = 0; }
void push(int value);
int pop(void) {
return stackstore[--SP];
}
};
void Stack::push(int value) {
stackstore[SP++] = value;
}
In this case I want to understand what difference it makes initializing SP in the constructor to doing it directly when declaring the variable. Why is it a better practice to do it the way depicted above rather than this way (without the constructor) ->:
class Stack {
private:
int stackstore[100];
int SP = 0;
public:
void push(int value);
int pop(void) {
return stackstore[--SP];
}
};
void Stack::push(int value) {
stackstore[SP++] = value;
}
Thank you in advance for any help :)

Related

Using 'override' keyword

My code gives the following error:
error C3668: 'B::getData': method with override specifier 'override' did not override any base class methods
#include<iostream>
#include <tuple>
using namespace std;
class A {
public:
int a;
int getData() {
return a;
}
};
class B : public A {
public:
int b;
B() {
b = 100;
}
int getData() override {
return b;
}
};
int main() {
B b;
cout << b.getData() << endl;
}
Why does error occurs and how can I resolve it ??
Your original function in A has to be virtual to be overrided.
class A {
public:
int a;
virtual int getData() {
return a;
}
};
More info about override is here. And related: info on virtual and final

Setter methods do not work whereas Getter methods work fine

The error is always that the dino cannot be converted into type string
however im struggling to understand why the compiler would think of trying to
converting dino into ints or strings from the first method
public static void Tyrannosaurus()
{
String DinoName = Name();
Dinosaur Tyrannosaurus = new Dinosaur();
Tyrannosaurus.name = DinoName;
Tyrannosaurus = setDiet(Tyrannosaurus, DinoDiet[0]);
Tyrannosaurus = setHP(Tyrannosaurus, 100);
Tyrannosaurus = setDamage(Tyrannosaurus, 200);
DaysLoop(DinoName);
return;
}
Here is the first getter method being used for the Dinosaur record and Tyrannosaurus instance above
public static String getName (Dinosaur dino)
{
return Tyrannosaurus.name;
}
public static String getDiet (Dinosaur dino)
{
return Tyrannosaurus.diet;
}
public static int getHP (Dinosaur dino)
{
return dino.HP;
}
public static int getDamage (Dinosaur dino)
{
return dino.damage;
}
setter method which does not work is done from here, I do see in other java setters people use this. but I havent quite grasped that concept yet
public static String setDiet (Dinosaur dino, String TyranDiet)
{
dino.diet = TyranDiet;
return dino;
}
public static int setHP (Dinosaur dino, int TyranHP)
{
dino.HP = TyranHP;
return dino;
}
public static int setDamage (Dinosaur dino, int TyranDamage)
{
dino.damage = TyranDamage;
return dino;
}
//////////////////////////////////
You return dino as string and dino as an int in below methods.
public static String setDiet (Dinosaur dino, String TyranDiet);
public static int setHP (Dinosaur dino, int TyranHP);
public static int setDamage (Dinosaur dino, int TyranDamage);
that's why its pops error like that
basically setters not returns anything. remove return and re-write code like this
public static void setDiet (Dinosaur dino, String TyranDiet)
{
dino.diet = TyranDiet;
}
public static void setHP (Dinosaur dino, int TyranHP)
{
dino.HP = TyranHP;
}
public static void setDamage (Dinosaur dino, int TyranDamage)
{
dino.damage = TyranDamage;
}

Singleton getinstance() method with void as return type

greetings.
I wrote the singleton class as shown below.
#include <iostream>
using namespace std;
class Singleton
{
private:
static bool inst;
static Singleton * ptr;
Singleton()
{
cout<<"Singleton Private Constructor is called"<<endl;
}
public:
static Singleton * Create_Instance()
{
if(!inst)
{
ptr = new Singleton();
inst = true;
cout<<"New instance is created"<<endl;
}
return ptr;
}
};
bool Singleton::inst = false;
Singleton * Singleton::ptr = NULL;
int main()
{
Singleton * point = Singleton::Create_Instance();
return 0;
}
Here the Singleton instance is returned to main from Create_Instance() method.
Here my is what if the return value of Create_Instance() is void ? Meaning if the signature is going to be "static void Create_Instance()", then how we will get the instance of Singleton in main.
Please help me in this regard.

Using input processor touchdown event to detect an object touch

I want to collect coin on a touching the coin object.So i did like this.
if (Gdx.input.justTouched()) {
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
game.camera.unproject(touchPos);
for (int i = 0; i < coins.length; i++) {
Rectangle textureBounds = new Rectangle(coins[i].getX(), coins[i].getY(), coins[i].getWidth(),coins[i].getHeight());
if (textureBounds.contains(touchPos.x, touchPos.y) && !coins[i].isBreakBool() && coins[i].isCoinVisible()) {
//after touch something happens
}
}
}
}
I increments the score and everything is proper except the touch.I want to make the coin invisible/moving immediately after touching down.This is not happening with justTouched().
So I want to use input processor touch down event for this.
I have my input processor class with touch down event like this.
public class MyInputProcessor implements InputProcessor,GestureListener {
public static boolean isTouchDown=false;
public static boolean isTouchUp=false;
public static boolean isTap=false;
public static boolean isLongPress=false;
public static boolean isFling=false;
public static boolean isSwipeDown=false;
public static boolean isSwipeUp=false;
public static boolean isSwipeLeft=false;
public static boolean isSwipeRight=false;
public static boolean isZoomed=false;
public static float zoomInitDist=0;
public static float zoomDist=0;
public MyInputProcessor() {
// TODO Auto-generated constructor stub
System.out.println("My Input Processor Created..");
}
public InputMultiplexer returnInput() {
// TODO Auto-generated method stub
InputMultiplexer im = new InputMultiplexer();
GestureDetector gd = new GestureDetector(this);
im.addProcessor(gd);
im.addProcessor(this);
return im;
}
#Override
public boolean touchDown(float x, float y, int pointer, int button) {
// TODO Auto-generated method stub
this.isTouchDown=true;
return true;
}
#Override
public boolean tap(float x, float y, int count, int button) {
// TODO Auto-generated method stub
this.isTap=true;
return true;
}
#Override
public boolean longPress(float x, float y) {
// TODO Auto-generated method stub
this.isLongPress=true;
return true;
}
#Override
public boolean fling(float velocityX, float velocityY, int button) {
// TODO Auto-generated method stub
if(Math.abs(velocityX)>Math.abs(velocityY)){
if(velocityX>0){
this.isSwipeRight=true;
}else{
this.isSwipeLeft=true;
}
}else{
if(velocityY>0){
this.isSwipeDown=true;
}else{
this.isSwipeUp=true;
}
}
return true;
}
#Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean panStop(float x, float y, int pointer, int button) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean zoom(float initialDistance, float distance) {
// TODO Auto-generated method stub
this.isZoomed=true;
this.zoomInitDist=initialDistance;
this.zoomDist=distance;
return true;
}
#Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
// TODO Auto-generated method stub
return true;
}
#Override
public void pinchStop() {
// TODO Auto-generated method stub
}
#Override
public boolean keyDown(int keycode) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean keyUp(int keycode) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean keyTyped(char character) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean scrolled(int amount) {
// TODO Auto-generated method stub
return true;
}
}
Inside gameScreen class,
MyInputProcessor myInputProcessor = new MyInputProcessor();
InputMultiplexer im = myInputProcessor.returnInput(stage);
Gdx.input.setInputProcessor(im);
But I am confused of how to get this touch position on touchdown event of input processor.
Your MyInputProcessor should be like this.
public class MyInputProcessor implements InputProcessor {
Vector3 touchPos;
GameScreen gameScreen;
public static InputMultiplexer getMux(GameScreen gameScreen){
InputMultiplexer im = new InputMultiplexer();
im.addProcessor(gameScreen.stage);
im.addProcessor(new MyInputProcessor(gameScreen));
return im;
}
public MyInputProcessor(GameScreen gameScreen){
touchPos=new Vector3();
this.gameScreen=gameScreen; // keep reference to access data member of GameScreen
}
#Override
public boolean keyDown(int keycode) {
return false;
}
#Override
public boolean keyUp(int keycode) {
return false;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
touchPos.set(screenX,screenY,0);
gameScreen.game.camera.unproject(touchPos);
Coin coins[]=gamescreen.coins;
for (int i = 0; i < coins.length; i++) {
Rectangle textureBounds = new Rectangle(coins[i].getX(), coins[i].getY(), coins[i].getWidth(),coins[i].getHeight());
if (textureBounds.contains(touchPos.x, touchPos.y) && !coins[i].isBreakBool() && coins[i].isCoinVisible()) {
//after touch something happens
}
}
return false;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolled(int amount) {
return false;
}
}
Set it as InputProcessor inside your GameScreen class.
Gdx.input.setInputProcessor(MyInputProcessor.getMux(this));

Problem with design in OOP (Virtual member call in constructor)

I am trying to achieve something like the following:
class Foo
{
public virtual int Number { get; set; }
public Foo(int n)
{
Number = n; //Virtual member call in constructor
}
public void Do() {
Console.WriteLine(Number);
}
}
class Bar : Foo
{
public override int Number
{
get
{
return x.Val;
}
set
{
x.Val = value;
}
}
Bar(int n) : base(n)
{
X x = new X();
x.Val = n;
}
public void F() {
x.Something(); //changes x.Val
}
}
The reason I am doing this is because I need to propagate the call to Do when called from a variable of type Bar.
Now, I can have objects that either inherit from Foo or Bar, thus Number needs to be the way it is now, ie directly expose the Val property of x. This is because I need to allow for the following code:
Bar b = new Bar(5);
b.F(); //changes the value of Val in x
b.Do(); //Needs to print the correct, modified value
The problem here is obviously in Foo, when assigning n to Number (Virtual member call in constructor) since x from the subclass would not have been initialized yet.
What do you think is a better way to structure such a design?
I can't use this current code because I am calling a virtual member from the constructor, which causes the Object Reference not set to an Instance of an Object exception since x in Number from Bar would not have been yet initialized.
My favorite quote:
Favor composition over inheritance
I would separate underlying data from operations:
interface IMyNumber
{
int Number { get; set; }
void Something();
}
class MyNumber : IMyNumber
{
public int Number { get; set; }
public MyNumber(int n)
{
Number = n;
}
void Something() {... }
}
class MyBoxedNumber : IMyNumber
{
int Number { get { ... } set {... } }
void Something() {... }
}
class Bar
{
private IMyNumber_foo;
Bar(IMyNumber foo)
{
_foo = foo;
}
public void F() {
_foo.Something(); //changes x.Val
}
public void Do() {
Console.WriteLine(...)
}
}