c++ windows forms l11 - inheritance
DESCRIPTION
C++ Windows Forms L11 - Inheritance of C++ Windows Forms Light CourseTRANSCRIPT
Mohammad Shakermohammadshakergtr.wordpress.com
C++.NET Windows Forms Course@ZGTRShaker
C++.NET Windows Forms Course
L11-Inheritance
Inheritancethe concept
Inheritance
• Now, let’s have the following class:
#pragma once
using namespace::System;
ref class MyClass
{
public:
MyClass(void);
virtual String ^ToString() override;
};
Inheritance
#include "StdAfx.h"
#include "MyClass.h"
MyClass::MyClass(void)
{}
String^ MyClass::ToString()
{
return "I won't red 3leek :P:P ";
};
Inheritance
• What happens? private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e)
{
MyClass ^MC = gcnew MyClass;
textBox1->Text = MC->ToString();
}
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;
};
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);
};
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();
}
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?
Inheritance
#pragma once
using namespace System;
ref class MyClass : System::Windows::Forms::Button
{
public:
MyClass(void);
};
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
Inheritance
• .cpp file
#include "StdAfx.h"
#include "MyClass.h"
namespace dotNet8_Inher {
MyClass::MyClass(void)
{}
}
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;
}
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!";
}
}
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;
}
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;
}
Inheritance
A way to steal :D
Multiple InheritanceThe concept
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);
};
}
#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;
}
Compiler errorAmbiguous, Why?
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)
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 ^%);
};
}
#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;
}
}
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);
}
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);
};
}
#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);
}
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)
{
}
}
Multiple Inheritance - DNDI
After pressing the button
Keep in touch:[email protected]
http://mohammadshakergtr.wordpress.com/tweet @ZGTRShaker
Go have some fun!