39
Mohammad Shaker mohammadshakergtr.wordpress.com C++.NET Windows Forms Course @ZGTRShaker C++.NET Windows Forms Course L11-Inheritance

C++ Windows Forms L11 - Inheritance

Embed Size (px)

DESCRIPTION

C++ Windows Forms L11 - Inheritance of C++ Windows Forms Light Course

Citation preview

Page 1: C++ Windows Forms L11 - Inheritance

Mohammad Shakermohammadshakergtr.wordpress.com

C++.NET Windows Forms Course@ZGTRShaker

C++.NET Windows Forms Course

L11-Inheritance

Page 2: C++ Windows Forms L11 - Inheritance
Page 3: C++ Windows Forms L11 - Inheritance
Page 4: C++ Windows Forms L11 - Inheritance
Page 5: C++ Windows Forms L11 - Inheritance

Inheritancethe concept

Page 6: C++ Windows Forms L11 - Inheritance

Inheritance

• Now, let’s have the following class:

#pragma once

using namespace::System;

ref class MyClass

{

public:

MyClass(void);

virtual String ^ToString() override;

};

Page 7: C++ Windows Forms L11 - Inheritance

Inheritance

#include "StdAfx.h"

#include "MyClass.h"

MyClass::MyClass(void)

{}

String^ MyClass::ToString()

{

return "I won't red 3leek :P:P ";

};

Page 8: C++ Windows Forms L11 - Inheritance

Inheritance

• What happens? private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

MyClass ^MC = gcnew MyClass;

textBox1->Text = MC->ToString();

}

Page 9: C++ Windows Forms L11 - Inheritance

Inheritance

• Let’s get it bigger a little

#pragma once

using namespace::System;

ref class MyClass

{

public:

MyClass(String^ s1, String^ s2);

virtual String ^ToString() override;

private:

String ^FName;

String ^LName;

};

Page 10: C++ Windows Forms L11 - Inheritance

Inheritance

#include "StdAfx.h"

#include "MyClass.h"

MyClass::MyClass(String^ s1, String^ s2)

{

FName = s1; LName = s2;

}

String^ MyClass::ToString()

{

return String::Format("{0}{1}{2}{3}", "My name is : ",

FName, " ", LName);

};

Page 11: C++ Windows Forms L11 - Inheritance

Inheritance

• What happens?

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

MyClass ^MC = gcnew MyClass("MeMe", "Auf-Wiedersehen");

textBox1->Text = MC->ToString();

}

Page 12: C++ Windows Forms L11 - Inheritance

Inheritance

• Let’s have the following ref class.• Everything ok?

#pragma once

ref class MyClass : System::Windows::Forms::Button

{

public:

MyClass(void);

};

Compiler error, why?

Page 13: C++ Windows Forms L11 - Inheritance

Inheritance

#pragma once

using namespace System;

ref class MyClass : System::Windows::Forms::Button

{

public:

MyClass(void);

};

Page 14: C++ Windows Forms L11 - Inheritance

Inheritance

• Why doing this?

#pragma once

using namespace System;

namespace dotNet8_Inher {

ref class Form1;

ref class MyClass : System::Windows::Forms::Button

{

public: MyClass(void);

};

}

Forward declaration

Page 15: C++ Windows Forms L11 - Inheritance

Inheritance

• .cpp file

#include "StdAfx.h"

#include "MyClass.h"

namespace dotNet8_Inher {

MyClass::MyClass(void)

{}

}

Page 16: C++ Windows Forms L11 - Inheritance

Inheritance

• In Form1.h

• What will happen now?

private: System::Void Form1_Load(System::Object^

sender, System::EventArgs^ e)

{

MyClass ^MC = gcnew MyClass;

MC->Parent = this;

}

• In Form1.h

• Sth needed? \ for Controls?

private: System::Void Form1_Load(System::Object^

sender, System::EventArgs^ e)

{

MyClass ^MC = gcnew MyClass;

}

Page 17: C++ Windows Forms L11 - Inheritance

Inheritance

Page 18: C++ Windows Forms L11 - Inheritance

Inheritance

• Now, let’s have the following in cpp file#include "StdAfx.h"

#include "MyClass.h"

namespace dotNet8_Inher {

MyClass::MyClass(void)

{

this->Text = "I'M HAPPY!";

}

}

Page 19: C++ Windows Forms L11 - Inheritance

Inheritance

• Again, in Form1.h

• What will happen now?

private: System::Void Form1_Load(System::Object^

sender, System::EventArgs^ e)

{

MyClass ^MC = gcnew MyClass;

MC ->Parent = this;

}

Page 20: C++ Windows Forms L11 - Inheritance

Inheritance

Page 21: C++ Windows Forms L11 - Inheritance

#include "StdAfx.h"

#include "MyClass.h"

namespace dotNet8_Inher {

MyClass::MyClass(void)

{

this->Location = System::Drawing::Point(223, 121);

this->Name = L"button1";

this->Size = System::Drawing::Size(75, 23);

this->TabIndex = 0;

this->Text = L"My Button!";

this->UseVisualStyleBackColor = true;

}

}

private: System::Void Form1_Load(System::Object^ sender,

System::EventArgs^ e)

{

MyClass ^MC = gcnew MyClass;

MC ->Parent = this;

}

Page 22: C++ Windows Forms L11 - Inheritance

Inheritance

Page 23: C++ Windows Forms L11 - Inheritance

A way to steal :D

Page 24: C++ Windows Forms L11 - Inheritance

Multiple InheritanceThe concept

Page 25: C++ Windows Forms L11 - Inheritance

Multiple Inheritance

• Now, let’s see the following crazy code

#pragma once

using namespace System;

namespace dotNet8_Inheir {

ref class Form1;

ref class MyClass : System::Windows::Forms::Button,

System::Windows::Forms::ComboBox

{

private:

Form1 ^MyForm;

public:

MyClass(Form1 ^f);

};

}

Page 26: C++ Windows Forms L11 - Inheritance

#include "StdAfx.h"

#include "MyClass.h"

namespace dotNet8_Inheir {

MyClass::MyClass(void)

{

this->Location = System::Drawing::Point(223, 121);

this->Name = L"button1";

this->Size = System::Drawing::Size(75, 23);

this->TabIndex = 0;

this->Text = L"My Button!";

this->UseVisualStyleBackColor = true;

}

}

private: System::Void Form1_Load(System::Object^ sender,

System::EventArgs^ e)

{

MyClass ^MC = gcnew MyClass;

MC ->Parent = this;

}

Page 27: C++ Windows Forms L11 - Inheritance

Compiler errorAmbiguous, Why?

Page 28: C++ Windows Forms L11 - Inheritance
Page 29: C++ Windows Forms L11 - Inheritance

Multiple Inheritance

• Why? – Can’t know the “location” peoperties is for! – Can’t inhert from more than one base class in.NET!

• So, what to do?– Dump fix. (Do Not Do it (DNDI) unlsess necessary)

Page 30: C++ Windows Forms L11 - Inheritance

Multiple Inheritance - DNDI#pragma once

using namespace System;

namespace dotNet8_Inheir {

ref class Form1;

ref class MyClass : public System::Windows::Forms::Button

{

private:

Form1 ^MyForm;

System::Windows::Forms::ComboBox ^MyCB;

public:

MyClass(Form1 ^f);

void InitializeButton();

void

InitializeComboBox(System::Windows::Forms::ComboBox ^%);

};

}

Page 31: C++ Windows Forms L11 - Inheritance

#include "StdAfx.h"

#include "MyClass.h”

namespace dotNet8_Inheir

{

MyClass::MyClass(Form1 ^f)

{

MyForm = f;

InitializeButton();

InitializeComboBox(MyCB);

}

void MyClass::InitializeButton()

{

this->Location = System::Drawing::Point(223, 121);

this->Name = L"button1";

this->Size = System::Drawing::Size(75, 23);

this->TabIndex = 0;

this->Text = L"My Button!";

this->UseVisualStyleBackColor = true;

}

void MyClass::InitializeComboBox(System::Windows::Forms::ComboBox ^%CB)

{

CB = gcnew System::Windows::Forms::ComboBox;

CB->Location = System::Drawing::Point(223, 121);

CB->Size = System::Drawing::Size(75, 23);

CB->TabIndex = 0;

CB->Text = L"My ComboBox!";

CB->Parent = this;

}

}

Page 32: C++ Windows Forms L11 - Inheritance
Page 33: C++ Windows Forms L11 - Inheritance

Multiple Inheritance - DNDI

• Let’s start all over again

private: System::Void Form1_Load(System::Object^ sender,

System::EventArgs^ e)

{

MyClass ^MC;

MC = gcnew MyClass(this);

}

Page 34: C++ Windows Forms L11 - Inheritance

Multiple Inheritance - DNDI#pragma once

using namespace System;

namespace dotNet8_Inheir {

ref class Form1;

ref class MyClass : public System::Windows::Forms::Button

{

private:

Form1 ^MyForm;

System::Windows::Forms::ComboBox ^MyCB;

public:

MyClass(Form1 ^f);

void InitializeButton();

void InitializeComboBox(System::Windows::Forms::ComboBox ^%);

void PlayIt(System::Object^ sender, System::EventArgs^ e);

void comboBox1_SelectedIndexChanged(System::Object^ sender,

System::EventArgs^ e);

};

}

Page 35: C++ Windows Forms L11 - Inheritance

#include "StdAfx.h"

#include "MyClass.h"

#include "Form1.h"

namespace dotNet8_Inheir

{

MyClass::MyClass(Form1 ^f)

{

MyForm = f;

this->Parent = MyForm;

InitializeButton();

InitializeComboBox(MyCB);

}

void MyClass::InitializeButton()

{

this->Button::Location = System::Drawing::Point(223, 121);

this->Name = L"button1";

this->Size = System::Drawing::Size(75, 23);

this->TabIndex = 0;

this->Text = L"My Button!";

this->UseVisualStyleBackColor = true;

this->Click += gcnew System::EventHandler(this, &MyClass::PlayIt);

}

Page 36: C++ Windows Forms L11 - Inheritance

void MyClass::InitializeComboBox(System::Windows::Forms::ComboBox ^%CB)

{

CB = gcnew System::Windows::Forms::ComboBox;

CB->FormattingEnabled = true;

CB->Location = System::Drawing::Point(100, 100);

CB->Size = System::Drawing::Size(121, 21);

CB->TabIndex = 2;

MyCB->Parent = MyClass::Parent;

CB->SelectedIndexChanged += gcnew System::EventHandler(this,

&MyClass::comboBox1_SelectedIndexChanged);

}

void MyClass::PlayIt(System::Object^ sender, System::EventArgs^ e)

{

Drawing::Point P = (dynamic_cast <Button^> (sender))->Location;

MyCB->Location = P;

}

void MyClass::comboBox1_SelectedIndexChanged(System::Object^ sender,

System::EventArgs^ e)

{

}

}

Page 37: C++ Windows Forms L11 - Inheritance

Multiple Inheritance - DNDI

After pressing the button

Page 38: C++ Windows Forms L11 - Inheritance

Keep in touch:[email protected]

http://mohammadshakergtr.wordpress.com/tweet @ZGTRShaker

Page 39: C++ Windows Forms L11 - Inheritance

Go have some fun!