AbstractStack is a integer stack class and an abstract class - stack

class AbstractStack
{
public :
virtual bool push(int n) = 0;// push n to the stack. If the stack is full, return false
virtual bool pop(int& n) = 0;// store the integer that popped from the stack to n,if the stack is empty return false.
virtual int size() = 0; //return number of integers that has been stored to the stack
}
Inheriting class AbstractStack, how can I create "class IntStack" where it 'pushes' and 'pops' integer?
I've so far tried
class IntStack : public AbstractStack{
bool push(int n);
bool pop(int &n);
}

Because there's a few pure-virtual methods (that's what the = 0 does) this means that any derived class must implement these methods.
AbstractStack defines an interface to manipulate your user-defined Stack types. This means that the code can use the same syntax to access the different types of Stack with different underlying types.
So given this:
class AbstractStack{
public :
virtual bool push(int n) = 0;
virtual bool pop(int& n) = 0;
virtual int size() = 0;
};
You would want to do something like this:
class IntStack: public AbstractStack{
public:
bool push(int n){
//implement the push() for Ints here
}
bool pop(int& n){
//implement the pop() for Ints here
}
int size(){
//implement the size() for Ints here
}
private:
//Here you need to specify the underlying storage type that this class uses
//to store the stack of int's
stack_t m_stack;
}; //Don't forget the trailing ;!

Related

Dart abstract static method

I want to create an abstract Sort class for my class project using dart which I will extend with Sorting algorithm classes like MergeSort, QuickSort, Heap, etc. I wrote the below code but I cannot create an abstract static sort method which I can override and use like
Heap.sort(arr)
OR
MergeSort.sort(arr)
Do anyone know why I cannot create abstract static method and also if you have any other way of doing this please feel free to guide me :D
abstract class Sort {
// void sort(List array);
static void sort(List array);
bool isSorted(List array) {
for (int i = 0; array.length > i - 1; i++) {
if (array[i] > array[i + 1]) {
return false;
}
}
return true;
}
void swap(arr, i, j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
As the answer linked above says, you can't have abstract static methods.
An abstract method does just one thing, it adds a method signature to the interface of the class, which other (non-abstract) classes implementing the interface then has to provide an implementation for. The abstract method doesn't add implementation.
Static methods are not part of the interface, so being abstract and static means it has no effect at all. It's a method with no implementation, which nobody can ever implement. So you're not allowed to do that.
To actually have separate classes representing different sorting algorithms, just do that directly using instance methods. That's the strategy object pattern.
abstract class Sorter<T> {
void sort(List<T> values);
int compare(T value1, T value2);
void swap(List<T> values, int index1, int index2) {
T tmp = values[index1];
values[index1] = values[index2];
values[index2] = tmp;
}
}
abstract class HeapSort<T> extends Sorter<T> {
void sort(List<T> values) {
// heap sort algorithm.
}
}
abstract class MergeSort<T> extends Sorter<T> {
void sort(List<T> values) {
// merge sort algorithm.
}
}
mixin ComparableSorter<T extends Comparable<T>> on Sorter<T> {
int compare(T value1, T value2) => value1.compareTo(value2);
}
class ComparableHeapSort<T extends Comparable<T>>
extends HeapSort<T> with ComparableSorter<T> {}
class CustomCompareHeapSort<T> extends HeapSort<T> {
int Function(T, T) _compare;
CustomCompareHeapSort(int Function(T, T) compare) : _compare = compare;
int compare(T value1, T value2) => _compare(value1, value2);
}
There are plenty of options about how to slice the API and abstract over different parts of it.
I recommend figuring out what use-cases you want to support, before starting the API design.

I want to know, in the code provided what does 'this' keyword referring to.?

/*
I know this keyword is used to refer to class members but i am unable to understand what does 'this'.isEmpty() referring to. Some class ? class method? Or some variable?
For example:
this.value = value;
I understand that here, this.value is referring to class variable value but not for previous occurence of 'this'.
*/
public class StackWithMin extends Stack< NodeWithMin > {
public void push(int value) {
int newMin = Math.min(value, min());
super.push(new NodeWithMin(value,newMin));
}
public int min() {
if(this.isEmpty()) {
return Integer.MAX_VALUE; // Error value
} else {
return peek().min;
}
}
}
class NodeWithMin {
public int value;
public int min;
public NodeWithMin(int value, int min) {
this.value = v;
this.min = min;
}
}
"this" here is object of the class StackWithMin which is extending Stack class from java.util. so StackWithMin is instance of Stack class.
this.isEmpty() which is method defined in Stack, here checks if the stack has any element or not. if it has zero element it returns true else false.
Hope it clears your doubt.

How to compare objects in Vala?

I am using a Gee.ArrayList with an own class for content. I want to use the "contains" method of the ArrayList, but I really don't know how to set up an equals-method in my class, so ArrayList uses it to find out if the object is in the ArrayList or not.
Example:
class Test : GLib.Object {
public int number;
public Test(int n) {
number = n;
}
public bool equals (Test other) {
if (number == other.number) return true;
return false;
}
}
Then, in another file:
var t = new Gee.ArrayList<Test>();
var n1 = new Test(3);
var n2 = new Test(3);
t.add(n1);
t.contains(n2); // returns false, but I want it to return true
Does anybody know that?
When you create the ArrayList, the constructor takes your equality comparator. You can do:
var t = new Gee.ArrayList<Test>(Test.equals);
and the contains should work as you desire.

Return int reference in vala

I have a class that has fields and I want call a method of this class and get the reference to one of the fields (not the value!!). Something like this:
class Test : Object{
uint8 x;
uint8 y;
uint8 z;
uint8 method(){
if (x == 1){
return y;
}else if (x == 2){
return z;
}
}
public static void main(string[] args){
uint8 i = method(); // get reference to y or z
i++; //this must update y or z
}
}
In C would be:
int& method()
{
if (x == 1){
return y;
}else if (x == 2){
return z;
}
}
How can I achieve this in vala?
Edit: I'm trying use pointers, I have the following
public class Test : Object {
private Struct1 stru;
struct Struct1{
uint8 _a;
public uint8 a{
get{ return _a; }
set{ _a = value; }
}
public Struct1(Struct1? copy = null){
if (copy != null){
this._a = copy.a;
}else{
this._a = 0;
}
}
public uint8* get_aa(){
return (uint8*)a;
}
}
public void get_pointer(){
uint8* dst = stru.get_aa();
}
public static int main (string[] args){
Test t = new Test();
return 0;
}
}
but when I compile I get
/home/angelluis/Documentos/vala/test.vala.c: In function ‘test_struct1_get_aa’:
/home/angelluis/Documentos/vala/test.vala.c:130:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
result = (guint8*) _tmp1_;
^
Compilation succeeded - 2 warning(s)
Why? I am returning an uint8* type and I attempt to store it in an uint8* pointer.
C doesn't have references (C++ does). Keep in mind that Vala compiles to C as an intermediate language.
I think that there are only two ways to do this in Vala:
Use a box type to encapsulate your uint8 values and return a reference to that box type.
Use a pointer. (Which opens the obvious pointer can of worms)
Edit: Answer to your updated example code problem:
You must be very careful with casting something to some pointer type. In this case the C compiler caught your spurious cast and emited a warning.
uint8 _a;
// This property will get and set the *value* of _a
public uint8 a{
get{ return _a; }
set{ _a = value; }
}
public uint8* get_aa(){
// Here you are casting a *value* of type uint8 to a pointer
// Which doesn't make any sense, hence the compiler warning
return (uint8*)a;
}
Note that you can't get a pointer or a reference to a property, because properties have no memory location on their own.
You can however get a pointer to the field _a in this case:
public uint8* get_aa(){
return &_a;
}
If you insist to go through the property, you have to make your property operate on the pointer as well:
uint8 _a;
public uint8* a{
get{ return &_a; }
}
Notice that in this version I have removed the get_aa () method which is now equivalent to the getter for a.
Also since in this code the property is returning a pointer there is no need for a setter, you can just dereference the pointer to set the value.

How does one obtain data that has been returned from a method? (Java)

This might be a really stupid question but what happens to data that is returned from a method? For example, if I have a method that adds two numbers and I tell it to return the sum, how would I access that information from the place where the method was called?
Assuming your question is related with java.
You could assign the whole method to a new variable.
public class Test {
public static void main(String args[]){
int value1=2;
int value2=5;
int sum=sum(value1,value2);
System.out.println("The sum is :"+ sum);
}
public static int sum(int value1,int value2){
return value1+value2;
}
}
What is actually happening, is that the method signature sum(value1,value2) holds the result of the 2 numbers summation. There is also another way of writing the code inside the method but the result will be the same.
For example:
public class Test {
public static void main(String args[]){
int sum=sum(2,5);
System.out.println("The sum is :"+ sum);
}
public static int sum(int value1,int value2){
int sum=value1+value2;
return sum;
}
}
P.S. You could try to use the above samples directly. They will compile and run.
In most languages, you access the result of a function by putting the function call on the right hand side of an assignment expression.
For example, in Python, you can assign the result of calling the built-in len function on a list to a variable called x by doing the following:
x = len([1, 2, 3])

Resources