Wednesday, July 10, 2019

copy constructor

#include<iostream>
#include<cstring>
using namespace std;

class String
{
private:
    char *s;
    int size;
public:
    String(const char *str = NULL); // constructor
    ~String() { delete [] s;  }// destructor
    String(const String&); // copy constructor
    void print() { cout << s << endl; } // Function to print string
    void change(const char *);  // Function to change
};

String::String(const char *str)
{
    size = strlen(str);
    s = new char[size+1];
    strcpy(s, str);
}

void String::change(const char *str)
{
    delete [] s;
    size = strlen(str);
    s = new char[size+1];
    strcpy(s, str);
}

String::String(const String& old_str)
{
cout << "copy constructor " << endl;
    size = old_str.size;
    s = new char[size+1];
    strcpy(s, old_str.s);
}

int main()
{
    String str1("GeeksQuiz");
    String str2 = str1;

    str1.print(); // what is printed ?
    str2.print();

    str2.change("GeeksforGeeks");

    str1.print(); // what is printed now ?
    str2.print();
    return 0;
}


Output:
GeeksQuiz
GeeksQuiz
GeeksQuiz 
GeeksforGeeks 

Friday, June 14, 2019

virtual function / pure virtual function example

#include <iostream>
using namespace std;

class Animal
{
public:
//        virtual void eat() { std::cout << "I'm eating generic food." << std::endl; }
    virtual void eat() =0; // pure virtual function ends with =0, this class cant be instantiate

    // turn the following virtual modifier on/off to see what happens
    //virtual
    std::string Says()
    {
        return "make virtual ?";
    }
};

class Cat : public Animal
{
public:
    void eat() override
    {
        std::cout << "I'm eating a rat." << std::endl;
    }
    void hi()
    {
        std::cout << "hiiii" << std::endl;
    }
    std::string Says()
    {
        return "Cat Woof";
    }
};

void func(Animal *xyz)
{
    xyz->eat();
//    xyz->hi();
}

int main()
{
//    base *p;
//    derived obj1;
//    p = &obj1;
//
    Animal *animal = new Animal;
    Cat *cat = new Cat;

//    animal->eat(); // Outputs: "I'm eating generic food."
//    cat->eat();    // Outputs: "I'm eating a rat."

//    func(animal); // Outputs: "I'm eating generic food."
    func(cat);    // Outputs: "I'm eating generic food."

    //////////////////////////
    Cat* c = new Cat();
    Animal* a = c;       // refer to Dog instance with Animal pointer

    cout << c->Says() << std::endl ;   // always Woof
    cout << a->Says() << std::endl;   // Woof or ?, depends on virtual

    return 0;
}


///////////////////////////////////////////
https://stackoverflow.com/questions/2391679/why-do-we-need-virtual-functions-in-c

Why virtual function


, assume for example a situation in which you had a function in your program that used the methods from each of the derived classes respectively(getMonthBenefit()):
double totalMonthBenefit = 0;    
std::vector<CentralShop*> mainShop = { &shop1, &shop2, &shop3, &shop4, &shop5, &shop6};
for(CentralShop* x : mainShop){
     totalMonthBenefit += x -> getMonthBenefit();
}
Now, try to re-write this, without any headaches!
double totalMonthBenefit=0;
Shop1* branch1 = &shop1;
Shop2* branch2 = &shop2;
Shop3* branch3 = &shop3;
Shop4* branch4 = &shop4;
Shop5* branch5 = &shop5;
Shop6* branch6 = &shop6;
totalMonthBenefit += branch1 -> getMonthBenefit();
totalMonthBenefit += branch2 -> getMonthBenefit();
totalMonthBenefit += branch3 -> getMonthBenefit();
totalMonthBenefit += branch4 -> getMonthBenefit();
totalMonthBenefit += branch5 -> getMonthBenefit();
totalMonthBenefit += branch6 -> getMonthBenefit();
And actually, this might be yet a contrived example either!

Thursday, May 30, 2019

Dynamic memory

But there may be cases where the memory needs of a program can only be determined during runtime.
For example, when the memory needed depends on user input. On these cases, 
programs need to dynamically allocate memory, for which the C++ language integrates the operators 
new and delete. 

Example: if array size needs to determine by user input that time we need dynamic memory. 
In normal array we need to declare the array size at the begining or in compile time. 
But in the dynamic memory allocation, user can define array size at the runtime of program





// rememb-o-matic
#include <iostream>
#include <new>
using namespace std;

int main ()
{
  int i,n;
  int * p;
  cout << "How many numbers would you like to type? ";
  cin >> i;
  p= new (nothrow) int[i];
  if (p == nullptr)
    cout << "Error: memory could not be allocated";
  else
  {
    for (n=0; n<i; n++)
    {
      cout << "Enter number: ";
      cin >> p[n];
    }
    cout << "You have entered: ";
    for (n=0; n<i; n++)
      cout << p[n] << ", ";
    delete[] p;
  }
  return 0;
}

different type of object

Header File: Point.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* The Point class Header file (Point.h) */
#ifndef POINT_H
#define POINT_H
 
class Point {
private:
   int x, y;  // Private data members
 
public:
   Point(int x = 0, int y = 0); // Constructor with default arguments
   int getX() const;  // Getter
   void setX(int x);  // Setter
   int getY() const;
   void setY(int y);
   void setXY(int x, int y);
   void print() const;
};
 
#endif
Implementation File: Point.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* The Point class Implementation file (Point.cpp) */
#include "Point.h"
#include <iostream>
using namespace std;
 
// Constructor - The default values are specified in the declaration
Point::Point(int x, int y) : x(x), y(y) { }
 
// Getters
int Point::getX() const { return x; }
int Point::getY() const { return y; }
 
// Setters
void Point::setX(int x) { this->x = x; }
void Point::setY(int y) { this->y = y; }
 
// Public Functions
void Point::setXY(int x, int y) { this->x = x; this->y = y; }
 
void Point::print() const {
   cout << "Point @ (" << x << "," << y << ")";
}
A Test Driver: TestPoint.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* A test driver program (TestPoint.cpp) */
#include "Point.h"
#include <iostream>
using namespace std;
 
int main() {
   // Instances (Objects)
   Point p1;       // Invoke default constructor
                   // OR Point p1 = Point(); NOT Point p1();
   Point p2(2, 2); // Invoke constructor
                   // OR Point p2 = Point(2, 2);
   p1.print();     // Point @ (0,0)
   cout << endl;
   p2.print();     // Point @ (2,2)
   cout << endl;
 
   // Object Pointers with dynamic allocation
   Point * ptrP3, * ptrP4; // Declare two Point pointers
   ptrP3 = new Point();    // Dynamically allocate storage via new
                           // with default constructor
   ptrP4 = new Point(4, 4);
   ptrP3->print();  // Point @ (0,0)
                    // prtPoint1->print() is the same as (*ptrP3).print()
   cout << endl;
   ptrP4->print();  // Point @ (4,4)
   cout << endl;
 
   delete ptrP3;    // Remove storage via delete
   delete ptrP4;
 
   // Object Reference (Alias)
   Point & p5 = p2; // Reference (alias) to an existing object
   p5.print();      // Point @ (2,2)
   cout << endl;
 
   /********************
    * ARRAYS           *
    ********************/
 
   // Array of Objects - Static Memory Allocation
   Point ptsArray1[2];     // Array of Point objects
                           // Use default constructor for all elements of the array
   ptsArray1[0].print();   // Point @ (0,0)
   cout << endl;
   ptsArray1[1].setXY(11, 11);
   (ptsArray1 + 1)->print(); // Point @ (11,11)
                             // same as ptsArray1[1].print()
   cout << endl;
 
   Point ptsArray2[3] = {Point(21, 21), Point(22, 22), Point()};
                           // Initialize array elements via constructor
   ptsArray2[0].print();   // Point @ (21,21)
   cout << endl;
   (ptsArray2 + 2)->print(); // Point @ (0,0)
                             // same as ptsArray2[2].print()
   cout << endl;
 
   // Array of Object Pointers - Need to allocate elements dynamically
   Point * ptrPtsArray3 = new Point[2];
   ptrPtsArray3[0].setXY(31, 31);
   ptrPtsArray3->print();  // Point @ (31,31)
                           // same as ptrPtsArray3[0].print()
   cout << endl;
   (ptrPtsArray3 + 1)->setXY(32, 32); // same as ptrPtsArray3[1].setXY(32, 32)
   ptrPtsArray3[1].print();           // Point @ (32,32)
   cout << endl;
 
   delete[] ptrPtsArray3; // Free storage
 
   // C++ does not support Array of References
// Point & pts[2] = {p1, p2};  // error: declaration of 'pts' as array of references
}

cpp class example

----------------------------------------------------------------------------------------
main.cpp
----------------------------------------------------------------------------------------

#include "array.h"
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main() {
    Array mArrayObj;

    // display 20 random numbers
    mArrayObj.display_num();

    // sort 20 random numbers in
    // asending order
    mArrayObj.asending_sort_array();

    cout << mArrayObj.get_min();  // get minimun number
    cout << " " << mArrayObj.get_max() << endl;  // get maximum number

    cout << mArrayObj.get_mean();
    cout << " " << mArrayObj.get_variance() << endl;

    return 0;
}

----------------------------------------------------------------------------------------
array.h
----------------------------------------------------------------------------------------


#ifndef Array_H
#define Array_H

class Array {
 private:
    unsigned int random_num[20];
    int get_array_length();

 public:
    Array();
    void display_num();
    int get_max();
    int get_min();
    float get_variance();
    float get_mean();
    void asending_sort_array();
};

#endif

----------------------------------------------------------------------------------------
array.cpp
----------------------------------------------------------------------------------------

#include "array.h"
#include <stdlib.h> /* srand, rand nummbers*/
#include <time.h>
#include <iostream>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;

Array::Array() {
    srand(time(NULL));
    // initialises all 20 numbers of array
    for (int i = 0; i < get_array_length(); i++) {
        random_num[i] = rand() % 100 + 0;  // 0 to 99 random number
    }
}

void Array::display_num() {
    for (int i = 0; i < get_array_length(); i++) {
        cout << random_num[i] << " ";
    }
    cout << endl;
}

int Array::get_max() {
    return random_num[get_array_length() - 1];
}

int Array::get_min() {
    return random_num[0];
}

float Array::get_variance() {
    float square_value = 0, variance = 0;
    for (int i = 0; i < get_array_length(); i++) {
        square_value += (random_num[i]-get_mean())*(random_num[i]-get_mean());
    }
    variance = square_value / get_array_length();

    return variance;
}

float Array::get_mean() {
    float average = 0, sum = 0;

    for (int i = 0; i < get_array_length(); i++) {
        sum = sum + random_num[i];
    }
    average = sum / get_array_length();

    return average;
}

int Array::get_array_length() {
    int length = (sizeof(random_num) / sizeof(random_num[0]));

    return length;
}

void Array::asending_sort_array() {
    std::sort(random_num, random_num + get_array_length());
}

Pointer example

#include<iostream>
#include <cmath>
using std::cin;
using std::cout;
using std::endl;

void doSomethingElsePointer(int* i);
void doSomethingElse(int i);
void doRef(int &i);
void array_pointer();

int main() { 

int i = 100;
    int *p = &i;
    doSomethingElse(i);  // passes i by references since doSomethingElse() receives it
                         // by reference, but the syntax makes it appear as if i is passed
                         // by value
                       
    cout << "doSomethingElse=" << i << endl;       
             
    doSomethingElsePointer(&i);
    doSomethingElsePointer(p);
    cout << "-----doSomethingElsePointer=" << i << endl; 
   
    doRef(i);
cout << "-----doRef after =" << i << endl;


array_pointer();


return 0;
}

void array_pointer()  // receives i as a reference
{
/////////////////////////////
int numbers[5];
  int * p;
  p = numbers; *p = 10;
  p++; *p = 20;
  p = &numbers[2];  *p = 30;
  p = numbers + 3;  *p = 40;
  p = numbers;  *(p+4) = 50;
  for (int n=0; n<5; n++)
    cout << numbers[n] << ", ";

}


void doSomethingElse(int i)  // receives i as a reference
{
i = 9;
    cout << i << endl;
}

void doSomethingElsePointer(int* i)
{
*i = 33;
    cout << "doSomethingElsePointer = "<< *i << endl;
}

void doRef(int &i)
{
i = 99;
    cout << "doRef="<<i << endl;
}

fibinacci series

#include <iostream>
#include <cmath>
using std::cin;
using std::cout;
using std::endl;

int main()
{
    unsigned num1 = 0, num2 = 1, nextNum = 0;
    int k;

    // taking input
    while (1) {
        cout << "k=";
        cin >> k;

        if (k > 1) {
            break;
        }
        else {
            // going to take input again
            cout << "k should be > 1" << endl;
            continue;
        }
    }

    for ( int i = 1; i <= k; ++i) {
        // first fibonacci number 0
        if (i == 1) {
            cout << num1 << " ";
            continue;
        }
        // second fibonacci number 1
        if (i == 2) {
            cout << num2 << " ";
            continue;
        }
        // for >=third number
        nextNum = num1 + num2;
        num1 = num2;
        num2 = nextNum;

        if (nextNum < k) {
            cout << nextNum << " ";
        }
    }
    return 0;
}