CPLUSPLUS abstract classes, polymorphism and structures


classes
Image credit: vapvarun.com

CPLUSPLUS abstract classes, polymorphism and structures
CPLUSPLUS abstract classes, polymorphism and structures are some of the very useful concepts in C++ programmming. Let’s see them one by one

Abstract classes
These classes provide further room for future members to participate. It is best to say that abstract classes provide a template for other derived classes to extend their functionality

A class that contains at least one pure virtual function is considered an abstract class. Classes derived from the abstract class must implement the pure virtual function or they, too, are abstract classes.
In abstract class, at least one virtual function is necessary.


#include"stdafx.h"
#include <iostream>
using namespace std;

class Polygon {
  protected:
    int width, height;
  public:
    void set_values (int a, int b)
      { width=a; height=b; }
};

class Rectangle: public Polygon {
  public:
    int area()
      { return width*height; }
};

class Triangle: public Polygon {
  public:
    int area()
      { return width*height/2; }
};

int main () {
  Rectangle rect;
  Triangle trgl;
  Polygon * ppoly1 = &rect;
  Polygon * ppoly2 = &trgl;
  ppoly1->set_values (4,5);
  ppoly2->set_values (4,5);
  cout << rect.area() << 'n';
  cout << trgl.area() << 'n';
  return 0;
}

Polymorphism
This technique is best described as the means of using the same method name but their arguments values are not the same.
They can be termed as ” One entity multiple existences”


#include iostream;
using namespace std;
 
class MyClass 
{
   public:
      void Display(int i) 
	  {
        cout &lt;&lt; "Integer type value " &lt;&lt; i &lt;&lt; endl;
      }

      void Display(double  f) {
        cout &lt;&lt; "Float type value: " &lt;&lt; f &lt;&lt; endl;
      }

      void Display(char* c) {
        cout &lt;&lt; "Character type value: " &lt;&lt; c &lt;&lt; endl;
      }
};

int main(void)
{
   MyClass obj;
 
   
   obj.Display(50);
   
   obj.Display(10000.25);
  
   obj.Display("Cool feature!!");
 
   return 0;
}

full credit http://www.cplusplus.com

Structures
Like classes, structures are also capable to store different data types.The basic difference between a class an structure

Structure vs class in C++

In C++, a structure is same as class except the following differences:

1) Members of a class are private by default and members of struct are public by default.
For example program 1 fails in compilation and program 2 works fine.

// Program 1
#include <stdio.h>
 
class Test {
    int x; // x is private
};
int main()
{
  Test t;
  t.x = 20; // compiler error because x is private
  getchar();
  return 0;
}
// Program 2
#include <stdio.h>
 
struct Test {
    int x; // x is public
};
int main()
{
  Test t;
  t.x = 20; // works fine because x is public
  getchar();
  return 0;
}

2) When deriving a struct from a class/struct, default access-specifier for a base class/struct is public. And when deriving a class, default access specifier is private.
For example program 3 fails in compilation and program 4 works fine.

// Program 3
#include <stdio.h>
 
class Base {
public:
    int x;
};
 
class Derived : Base { }; // is equilalent to class Derived : private Base {}
 
int main()
{
  Derived d;
  d.x = 20; // compiler error becuase inheritance is private
  getchar();
  return 0;
}
// Program 4
#include <stdio.h>
 
class Base {
public:
    int x;
};
 
struct Derived : Base { }; // is equilalent to struct Derived : public Base {}
 
int main()
{
  Derived d;
  d.x = 20; // works fine becuase inheritance is public
  getchar();
  return 0;
}

full credit:http://www.geeksforgeeks.org/g-fact-76/

The ref and sealed keywords

The sealed keyword denotes that the class can not be inherited. Ref, on the other hand denotes that the class support native code

Sealed class

The sealed keyword, when used with classes, indicates that the methods in a class can not be overridden and not even a class act as a base class

final keyword is used after the class name to make the class sealed
class A final  
{
   //...
};

Above class can not be derived.

ref indicates that the class implements native way of coding

Syed Adeel Ahmed
Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.