zoukankan      html  css  js  c++  java
  • Visual C++ 2008入门经典 第七章 自定义数据类型 简单

    // 第七章 自定义数据类型
    // 没有该章ppt,只能直接按实例学习了
    
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    using namespace System;
    
    //01自定义数据结构
    /*struct RECTANGLE
    {
         int Left;
    	 int Top;
    
    	 int Right;
    	 int Bottom;
    };
    //计算矩形的面积
    long Area(RECTANGLE& aRect);
    void MoveRect(RECTANGLE& aRect, int x, int y);
    int main(array<System::String ^> ^args)
    {
    
    	RECTANGLE Yard = {0, 0, 100, 120};
    	RECTANGLE Pool = {30, 40, 70, 80};
    	RECTANGLE Hut1, Hut2;
    
    	Hut1.Left = 70;
    	Hut1.Top = 10;
    	Hut1.Right = Hut1.Left + 25;
    	Hut1.Bottom = 30;
    
    	Hut2 = Hut1;
    	MoveRect(Hut2, 10, 90);
    
    	cout<<"移动 Hut2后:"<<Hut2.Left<<", "<<Hut2.Top<<" AND "<<Hut2.Right<<", "<<Hut2.Bottom<<endl;
    
    	cout<<"计算 Yard的面积是:"<<Area(Yard)<<endl;
    
    	cout<<"计算poll的面积是:"<<Area(Pool)<<endl;
        system("pause");
        return 0;
    }
    long Area(RECTANGLE& aRect)
    {
        return static_cast<long>((aRect.Right - aRect.Left) * (aRect.Bottom-aRect.Top));
    	//return 0.0;
    }
    //移动矩形
    void MoveRect(RECTANGLE& aRect, int x, int y)
    {
    	int length = aRect.Right - aRect.Left;
    	int width = aRect.Bottom - aRect.Top;
    
    	aRect.Left = x;
    	aRect.Top = y;
    	aRect.Right = x + length;
    	aRect.Bottom = y + width;
    	return;
    }
    */
    
    //02 创建和使用类
    /*class CBox
    {
    public:
    	double m_Length;
    	double m_Width;
    	double m_Height;
    };
    int main(array<System::String ^> ^args)
    {
        CBox box1;
    	CBox box2;
    
    	double boxVolume = 0.0;
    	box1.m_Height =18.0;
    	box1.m_Length = 78.0;
    	box1.m_Width = 24.0;
    
    	box2.m_Height = box1.m_Height - 10;  //8.0
    	box2.m_Length = box1.m_Length / 2.0; //39
    	box2.m_Width = box1.m_Length * 0.25; //19.5
    	boxVolume = box1.m_Height * box1.m_Length*box1.m_Width;
    
    	cout<<" Volume of box1 = "<<boxVolume<<endl;
    
    	cout<<" box2 has sides which total "<<box2.m_Height + box2.m_Length + box2.m_Width<<endl;
    
    	cout<<" A CBox object occupies "<<sizeof box1<<" bytes."<<endl;
    
    	system("pause");
    	return 0;
    }*/
    
    //03 一个成员函数的一个盒子的体积计算
    /*class CBox
    {
    public:
    	double m_Length;
    	double m_Width;
    	double m_Height;
    	
    	double Volume()
    	{
    	    return m_Length * m_Width * m_Height;
    	}
    };
    int main(array<System::String ^> ^args)
    {
        CBox box1;
    	CBox box2;
    
    	double boxVolume = 0.0;
    	box1.m_Height =18.0;
    	box1.m_Length = 78.0;
    	box1.m_Width = 24.0;
    
    	box2.m_Height = box1.m_Height - 10;  //8.0
    	box2.m_Length = box1.m_Length / 2.0; //39
    	box2.m_Width = box1.m_Length * 0.25; //19.5
    	
    	boxVolume = box1.Volume();
    
    	cout<<" Volume of box1 = "<<boxVolume<<endl;
    	cout<<" box2 has sides which total "<<box2.Volume()<<endl;
    	cout<<" A CBox object occupies "<<sizeof box1<<" bytes."<<endl;
    	system("pause");
    	return 0;
    }*/
    
    
    //04 使用构造函数
    /*class CBox
    {
    public:
    	double m_Length;
    	double m_Width;
    	double m_Height;
    	
    	CBox(double lv, double bv, double hv)
    	{
    	    m_Length = lv;
    		m_Width = bv;
    		m_Height = hv;
    	}
    
    	double Volume()
    	{
    	    return m_Length * m_Width * m_Height;
    	}
    };
    int main(array<System::String ^> ^args)
    {
        CBox box1(78.0, 24.0, 18.0);
    	CBox box2(8.0, 5.0, 1.0);
    	double boxVolume = 0.0;
    
    	boxVolume = box1.Volume();
    	cout<<"Volume of box1 = "<<boxVolume<<endl;
    
    	cout<<"Volume of box2 = "<<box2.Volume()<<endl;
    	system("pause");
    	return 0;
    }*/
    
    
    //05 默认构造函数
    /*class CBox
    {
    public:
    	double m_Length;
    	double m_Width;
    	double m_Height;
    	
    	CBox(double lv, double bv, double hv)
    	{
    		cout<<"带三个参数的构造函数"<<endl;
    	    m_Length = lv;
    		m_Width = bv;
    		m_Height = hv;
    	}
    	CBox()
    	{
    	    cout<<"默认构造函数"<<endl;
    	}
    
    	double Volume()
    	{
    	    return m_Length * m_Width * m_Height;
    	}
    };
    int main(array<System::String ^> ^args)
    {
        CBox box1(78.0, 24.0, 18.0);
    	CBox box2;
    	CBox cigarBox(8.0, 5.0, 1.0);
    	double boxVolume = 0.0;
    
    	boxVolume = box1.Volume();
    	cout<<"Volume of box1 = "<<boxVolume<<endl;
    
    	box2.m_Height = box1.m_Height - 10;
    	box2.m_Length = box1.m_Length / 2.0;
    	box2.m_Width = 0.25 * box1.m_Length;
    	cout<<"Volume of box2 = "<<box2.Volume()<<endl;
    
    	cout<<"Volume of cigarBox = "<<cigarBox.Volume()<<endl;
    	system("pause");
    	return 0;
    }*/
    
    //06 提供默认构造函数的参数值
    /*class CBox
    {
    public:
    	double m_Length;
    	double m_Width;
    	double m_Height;
    	
    	CBox(double lv=1.0, double bv=1.0, double hv=1.0)
    	{
    		cout<<"带三个参数的构造函数"<<endl;
    	    m_Length = lv;
    		m_Width = bv;
    		m_Height = hv;
    	}
    	double Volume()
    	{
    	    return m_Length * m_Width * m_Height;
    	}
    };
    int main(array<System::String ^> ^args)
    {
        CBox box2;
    	cout<<"Volume of box2 = "<<box2.Volume()<<endl;
        system("pause");
    	return 0;
    }*/
    
    
    //07 类的私有成员
    /*class CBox
    {
    public:	
    	CBox(double lv=1.0, double bv=1.0, double hv=1.0):m_Length(lv),m_Width(bv),m_Height(hv)
    	{
    	}
    	double Volume()
    	{
    	    return m_Length * m_Width * m_Height;
    	}
    private:
    	double m_Length;
    	double m_Width;
    	double m_Height;
    };
    int main(array<System::String ^> ^args)
    {
        CBox box2;
    	cout<<"Volume of box2 = "<<box2.Volume()<<endl;
    	
    	CBox match(2.2, 1.1, 0.5);
    	cout<<"Volume of match = "<<match.Volume()<<endl;
    	system("pause");
    	return 0;
    }*/
    
    //08  创建一类的友元函数
    /*class CBox
    {
    public:	
    	CBox(double lv=1.0, double bv=1.0, double hv=1.0):m_Length(lv),m_Width(bv),m_Height(hv)
    	{
    	}
    	double Volume()
    	{
    	    return m_Length * m_Width * m_Height;
    	}
    private:
    	double m_Length;
    	double m_Width;
    	double m_Height;
    	friend double BoxSurface(CBox aBox);
    };
    
    double BoxSurface(CBox aBox)
    {
        return 2.0 * (aBox.m_Length * aBox.m_Width + aBox.m_Length*aBox.m_Height + aBox.m_Height*aBox.m_Width);
    }
    
    int main(array<System::String ^> ^args)
    {
        CBox box2;
    	cout<<"Volume of box2 = "<<box2.Volume()<<endl;
    	cout<<"Surface area of box2 = "<<BoxSurface(box2)<<endl;
    
    	CBox match(2.2, 1.1, 0.5);
    	cout<<"Volume of match = "<<match.Volume()<<endl;
    	cout<<"Surface area of match = "<<BoxSurface(match)<<endl;
    
    
    	system("pause");
    	return 0;
    }*/
    
    
    //09 初始化一个对象的对象相同的类
    /*class CBox
    {
    public:	
    	CBox(double lv=1.0, double bv=1.0, double hv=1.0):m_Length(lv),m_Width(bv),m_Height(hv)
    	{
    	}
    	double Volume()
    	{
    	    return m_Length * m_Width * m_Height;
    	}
    private:
    	double m_Length;
    	double m_Width;
    	double m_Height;
    };
    
    int main(array<System::String ^> ^args)
    {
        CBox box1(78.0, 24.0, 18.0);
    	CBox box2 = box1;
    	cout<<"box1 volume = "<<box1.Volume()<<endl;
    	cout<<"box2 volume = "<<box2.Volume()<<endl;
    	system("pause");
    	return 0;
    }*/
    
    //10 使用指针
    /*class CBox
    {
    public:	
    	CBox(double lv=1.0, double bv=1.0, double hv=1.0):m_Length(lv),m_Width(bv),m_Height(hv)
    	{
    	}
    	double Volume()
    	{
    	    return m_Length * m_Width * m_Height;
    	}
    
    	//如果当前类大于xBox类的值返回true
    	int Compare(CBox xBox)
    	{
    	    return this->Volume() > xBox.Volume();
    	}
    
    private:
    	double m_Length;
    	double m_Width;
    	double m_Height;
    };
    
    int main(array<System::String ^> ^args)
    {
        CBox box1(2.2, 1.1, 0.5);
    	CBox box2(8.0, 5.0, 1.0);
    	if(box1.Compare(box2))
    	{
    	   cout<<"box1 大于 box2"<<endl;
    	}else{
    	   cout<<"box1 小于 box2"<<endl;
    	}
    	system("pause");
    	return 0;
    }*/
    
    
    // 11 使用数组类对象
    /*class CBox
    {
    public:	
    	//CBox(double lv=1.0, double bv=1.0, double hv=1.0)
    	//如果也像上面一样进默认值的话,那么跟下面的默认构造函数一样了
    	CBox(double lv, double bv=1.0, double hv=1.0)
    	{
    		m_Length = lv;
    		m_Width = bv;
    		m_Height = hv;
    	}
    
    	CBox()
    	{
    	   m_Length = m_Width = m_Height = 1.0;
    	}
    	//返回值不能修改的
    	double Volume()const
    	{
    	    return m_Length * m_Width * m_Height;
    	}
    	//如果当前类大于xBox类的值返回true
    	int Compare(CBox xBox)
    	{
    	    return this->Volume() > xBox.Volume();
    	}
    
    private:
    	double m_Length;
    	double m_Width;
    	double m_Height;
    };
    
    int main(array<System::String ^> ^args)
    {
        CBox boxes[5];
    	CBox cigar(8.0, 5.0, 1.0);
    	cout<<"Volume of boxes[3] = "<<boxes[3].Volume()<<endl;
    	cout<<"Volume of cigar = "<<cigar.Volume()<<endl;	
    	system("pause");
    	return 0;
    }*/
    
    //12 使用静态数据成员的类
    /*class CBox
    {
    public:	
    	static int ObjectCount;
    	//CBox(double lv=1.0, double bv=1.0, double hv=1.0)
    	//如果也像上面一样进默认值的话,那么跟下面的默认构造函数一样了
    	CBox(double lv, double bv=1.0, double hv=1.0)
    	{
    		m_Length = lv;
    		m_Width = bv;
    		m_Height = hv;
    		ObjectCount++;
    	}
    
    	CBox()
    	{
    	   m_Length = m_Width = m_Height = 1.0;
    	   ObjectCount++;
    	}
    	//返回值不能修改的
    	double Volume()const
    	{
    	    return m_Length * m_Width * m_Height;
    	}
    	//如果当前类大于xBox类的值返回true
    	int Compare(CBox xBox)
    	{
    	    return this->Volume() > xBox.Volume();
    	}
    
    private:
    	double m_Length;
    	double m_Width;
    	double m_Height;
    };
    int CBox::ObjectCount = 0;
    
    int main(array<System::String ^> ^args)
    {
        CBox boxes[5];
    	CBox cigar(8.0, 5.0, 1.0);
    	cout<<"Volume of boxes[3] = "<<boxes[3].Volume()<<endl;
    	cout<<"boxes[3] CBox::ObjectCount = "<<CBox::ObjectCount<<endl;
    	cout<<"Volume of cigar = "<<cigar.Volume()<<endl;	
    	cout<<"cigar CBox::ObjectCount = "<<cigar.ObjectCount<<endl;
    
    	system("pause");
    	return 0;
    }*/
    
    
    //13 行使间接成员访问运算符
    /*class CBox
    {
    public:	
    	static int ObjectCount;
    	//CBox(double lv=1.0, double bv=1.0, double hv=1.0)
    	//如果也像上面一样进默认值的话,那么跟下面的默认构造函数一样了
    	CBox(double lv, double bv=1.0, double hv=1.0)
    	{
    		m_Length = lv;
    		m_Width = bv;
    		m_Height = hv;
    		ObjectCount++;
    	}
    
    	CBox()
    	{
    	   m_Length = m_Width = m_Height = 1.0;
    	   ObjectCount++;
    	}
    	//返回值不能修改的
    	double Volume()const
    	{
    	    return m_Length * m_Width * m_Height;
    	}
    	//如果当前类大于xBox类的值返回true
    	int Compare(CBox xBox)
    	{
    	    return this->Volume() > xBox.Volume();
    	}
    
    private:
    	double m_Length;
    	double m_Width;
    	double m_Height;
    };
    int CBox::ObjectCount = 0;
    
    int main(array<System::String ^> ^args)
    {
        CBox boxes[5];
    	CBox match(2.2, 1.1, 0.5);
    	CBox cigar(8.0, 5.0, 1.0);
    
    	CBox* pB1 = &cigar;
    	CBox* pB2 = 0;
    
    	cout<<" Address of cigar is "<<pB1<<endl;
    	cout<<" Volume of cigar is "<<pB1->Volume()<<endl;
    
    	pB2 = &match;
    	cout<<" Address of match is "<<pB2<<endl;
    	cout<<" Volume of match is "<<pB2->Volume()<<endl;
    	if(pB2->Compare(*pB1)){
    	   cout<<"cigar 大于 match"<<endl;
    	}else{
    	   cout<<"cigar 小于 match"<<endl;
    	}
    
    	pB1 = boxes;
    	boxes[2] = match;
    	cout<<"Volume of boxes[2] is "<< (pB1 + 2)->Volume()<<endl;
    
    	system("pause");
    	return 0;
    }*/
    
    //14 定义和使用一个值类型
    /*value class Height
    {
    private:
    	int feet;
    	int inches;
    public:
    	Height(int ins)
    	{
    	   feet = ins / 12;
    	   inches = ins % 12;
    	}
    	Height(int ft, int ins) : feet(ft), inches(ins){}
    };
    
    int main(array<System::String ^> ^args)
    {
    	Height myHeight = Height(6,3);
    	Height^ yourHeight = Height(70);
    	Height hisHeight = *yourHeight;
    
    	Console::WriteLine(L"My height is {0}", myHeight); //这里咱出现的是类的名称,有点不清楚
    	Console::WriteLine(L"Your height is {0}", yourHeight);
    	Console::WriteLine(L"his height is {0}", hisHeight);
        system("pause");
        return 0;
    }*/
    
    
    /*value class Height
    {
    private:
    	int feet;
    	int inches;
    public:
    	Height(int ins)
    	{
    	   feet = ins / 12;
    	   inches = ins % 12;
    	}
    	Height(int ft, int ins) : feet(ft), inches(ins){}
    
    	virtual String^ ToString() override
    	{
    	   return feet+L" feet "+ inches + L" inches";
    	}
    };
    
    int main(array<System::String ^> ^args)
    {
    	Height myHeight = Height(6,3);
    	Height^ yourHeight = Height(70);
    	Height hisHeight = *yourHeight;
    
    	Console::WriteLine(L"My height is: {0}", myHeight); //这里咱出现的是类的名称,有点不清楚
    	Console::WriteLine(L"Your height is: {0}", yourHeight);
    	Console::WriteLine(L"his height is: {0}", hisHeight);
        system("pause");
        return 0;
    }*/
    
    
    //15 使用Box引用类类型
    /*ref class CBox
    {
    public:		
    	CBox(): m_Length(1.0), m_Width(1.0), m_Height(1.0)
    	{
    	   Console::WriteLine(L"No-arg constructor called.");
    	}
    	CBox(double lv, double bv, double hv):m_Length(lv), m_Width(bv), m_Height(hv)
    	{
    		Console::WriteLine(L"Constructor called.");
    	}
    	double Volume()
    	{
    	    return m_Length * m_Width * m_Height;
    	}
    private:
    	double m_Length;
    	double m_Width;
    	double m_Height;
    };
    
    
    int main(array<System::String ^> ^args)
    {
        CBox^ aBox;
    	CBox^ newBox = gcnew CBox(10, 15, 20);
    	aBox = gcnew CBox;
    	Console::WriteLine(L"Default box volume is {0}", aBox->Volume());
    	Console::WriteLine(L"New box volume is {0}", newBox->Volume());
    	system("pause");
    	return 0;
    }*/
    
    
    //16  使用标量属性
    /*value class Height
    {
    private:
    	int feet;
    	int inches;
    	
    	literal int inchesPerFoot = 12;
    	literal double inchesToMeters = 2.54 / 100;
    
    public:
    	Height(int ins)
    	{
    	    feet = ins / inchesPerFoot;
    		inches = ins % inchesPerFoot;
    	}
    
    	Height(int ft, int ins) : feet(ft), inches(ins){ }
    
    	property double meters
    	{
    		double get()
    		{
    		    return inchesToMeters * (feet * inchesPerFoot + inches);
    		}
    	}
    
    	virtual String^ ToString() override
    	{
    	    return feet + L" feet " + inches + L" inches ;";
    	}
    };
    
    value class Weight
    {
    private:
    	int lbs;
    	int oz;
    
    	literal int ouncesPerPound = 16;
    	literal double lbsToKg = 1.0 / 2.2;
    
    public:
    	Weight(int pounds, int ounces)
    	{
    	    lbs = pounds;
    		oz = ounces;
    	}
    
    	property int pounds
    	{
    		int get(){ return lbs;}
    		void set(int value){ lbs = value; }
    	}
    
    	property int ounces
    	{
    		int get(){ return oz;}
    		void set(int value){ oz = value;}
    	}
    
    	property double kilograms
    	{
    		double get(){ return lbsToKg*(lbs + oz/ouncesPerPound); }  
    	}
    
    	virtual String^ ToString() override
    	{
    	    return lbs + L" pounds " + oz + L" ounces ";
    	}
    };
    
    ref class Person
    {
    private:
    	Height ht;
    	Weight wt;
    public:
    	property String^ Name;
    
    	Person(String^ name, Height h, Weight w):ht(h),wt(w)
    	{
    	    Name = name;
    	}
    	Height getHeight(){ return ht;}
    	Weight getWeight(){ return wt;}
    };
    int main(array<System::String ^> ^args)
    {
    	Weight hisWeight = Weight(185, 7);
    	Height hisHeight = Height(6, 3);
    	Person^ him = gcnew Person(L"Fred", hisHeight, hisWeight);
    
    	Weight herWeight = Weight(105, 3);
    	Height herHeight = Height(5, 2);
    	Person^ her = gcnew Person(L"Freda", herHeight, herWeight);
    
    	Console::WriteLine(L"She is {0}", her->Name);
    	Console::WriteLine(L"Her weight is {0:F2} kilograms.", her->getWeight().kilograms);
    
    	Console::WriteLine(L"Her height is {0} which is {1:F2} meters.", her->getHeight(), her->getHeight().meters);
    
    
    	Console::WriteLine(L"He is {0}", him->Name);
    	Console::WriteLine(L"His weight is {0}.", him->getWeight());
    	Console::WriteLine(L"His height is {0} which is {1:F2} meters.", him->getHeight(), him->getHeight().meters);
    
    
        system("pause");
        return 0;
    }*/
    
    
    //17 默认的索引属性的定义和使用
    /*ref class Name
    {
    private:
    	array<String^>^ Names;
    public:
    	Name(...array<String^>^ names) : Names(names){ }
    	
    	property int NameCount
    	{
    		int get(){ return Names->Length;}
    	}
    
    	property String^ default[int]
    	{
    		String^ get(int index)
    		{
    		     if(index >= Names->Length)
    				 throw gcnew Exception(L"Index out of range");
    			 return Names[index];
    		}
    	}
    }; 
    int main(array<System::String ^> ^args)
    {
    	Name^ myName = gcnew Name(L"Ebenezer", L"Isaiah", L"Ezra", L"Inigo", L"Whelkwhistle");
    	for(int i=0; i< myName->NameCount; i++)
    	{
    		Console::WriteLine(L"Name {0} is {1}", i+1, myName[i]);
    	}
        system("pause");
        return 0;
    }*/
    
    //17A 随着集索引属性的能力增加
    ref class Name
    {
    private:
    	array<String^>^ Names;
    public:
    	Name(...array<String^>^ names) : Names(names){ }
    	
    	property int NameCount
    	{
    		int get(){ return Names->Length;}
    	}
    
    	property String^ default[int]
    	{
    		String^ get(int index)
    		{
    		     if(index >= Names->Length)
    				 throw gcnew Exception(L"Index out of range");
    			 return Names[index];
    		}
    
    		void set(int index, String^ name)
    		{
    		    if(index >= Names->Length)
    				throw gcnew Exception(L"Index out of range");
    			Names[index] = name;
    		}
    	}
    	property wchar_t Initials[int]
    	{
    		wchar_t get(int index)
    		{
    		     if(index >= Names->Length)
    				 throw gcnew Exception(L"Index out of range");
    			 return Names[index][0];		
    		}
    	}
    }; 
    int main(array<System::String ^> ^args)
    {
    	Name^ myName = gcnew Name(L"Ebenezer", L"Isaiah", L"Ezra", L"Inigo", L"Whelkwhistle");	
    	
    	myName[myName->NameCount - 1] = L"Oberwurst"; //修改最后一个值 这里应该是重载函数
    	
    	for(int i=0; i< myName->NameCount; i++)
    	{
    		Console::WriteLine(L"Name {0} is {1}", i+1, myName[i]);
    	}
    
    	Console::Write(L"The initials are:");
    	for(int i=0; i<myName->NameCount; i++)
    	{
    		Console::Write(L" {0}", myName->Initials[i]);
    	}
    	Console::WriteLine();
    
        system("pause");
        return 0;
    }
    
    /*int main(array<System::String ^> ^args)
    {
        Console::WriteLine(L"Hello World");
        return 0;
    }*/
    

      

  • 相关阅读:
    Android Studio的代码没错,运行时logcat会出现红色语句解决方法
    541. Reverse String II
    Android Studio快速自动生成findViewById
    你真的了解android的layout_weight属性吗?
    572. Subtree of Another Tree
    441. Arranging Coins(可用二分搜索)
    67. Add Binary
    58. Length of Last Word
    724. Find Pivot Index
    我爱java系列--【加密算法的介绍】
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/2744113.html
Copyright © 2011-2022 走看看