Home > Development > Abstract Methods and Classes in C# and C++

Abstract Methods and Classes in C# and C++

Well here we again for our third post today. We are going to look at the diffrences between C# and C++ classes.
Okay so what just is an abstract class?
Well here is the definition from Wikipedia 🙂

“In programming languages, an abstract type is a type in a nominative type system which is declared by the programmer. It may or may not include abstract methods or properties that contains members which are also shared members of some declared subtype. In many object oriented programming languages, abstract types are known as abstract base classes, interfaces, traits, mixins, flavors, or roles. Note that these names refer to different language constructs which are (or may be) used to implement abstract types.
Two overriding characteristics of abstract classes is that their use is a design issue in keeping with the best object oriented programming practices, and by their nature are unfinished.”

Really abstract classes can define some implementation but really require the class to be subclassed. I really use these when I want some definition in a base class with some methods that have no implementation that need to be provided in the derived class. When in doubt if no fields or state is required but just property or method definition prefer interfaces.

using System;

namespace CSharpClasses

	public abstract class Shape
		protected string shapename;
		public Shape(string shapeName){
			this.shapename = shapeName;
		public abstract void DisplayName();
	public class Rectangle : Shape{
		public Rectangle() : base("Rectange"){}
		public override void DisplayName ()
			Console.WriteLine("Awesome: {0}", shapename);

	class MainClass
		public static void Main (string[] args)
			Shape s = new Rectangle();

The definition in C++ in not so nice. There is no abstract keyword it is implied when virtual methods have the = 0 on the end.

#include <iostream>
#include <string>
#include <typeinfo>

using namespace std;

class Shape{
    string shapename;
    Shape(const string shapeName);
    virtual void displayName() = 0;

Shape::Shape(string shapeName){
    this->shapename = shapeName;

class Rectange : public Shape{
    Rectange(const string shapename);
    virtual void displayName();

Rectange::Rectange(const string shapename) : Shape(shapename){

void Rectange::displayName(){
    cout << "Awesome: " << shapename << endl;

int main(int argc, char *argv[])
    Shape* s = new Rectange("Rectange");
    delete s;

        Shape test("Rectange");
        Wont compile as Shape has pure virtual functions - what c++ calls abstract methods
    return 0;

Lets look at constructors in detail in my my next post


Categories: Development Tags: , , , , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: