Adding Constructors : Constructor « Class « Visual C++ .NET

Home
Visual C++ .NET
1.2D
2.Class
3.Collections
4.Data Type
5.Database ADO.net
6.Delegate
7.Development
8.File Directory
9.Function
10.Generics
11.GUI Form
12.Language Basics
13.Network
14.Reflection
15.Security
16.Statement
17.Structure
18.Thread
19.XML
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Visual C++ .NET » Class » Constructor 
Adding Constructors
 
#include "stdafx.h"
using namespace System;

ref class ParentClass{
public:
    ParentClass() : PVal(10) {}
    ParentClass(int inVal: PVal(inVal) {}
    ParentClass(const ParentClass %p: PVal(p.PVal) {}

    int PVal;
};

ref class ChildClass : public ParentClass{
public:
    ChildClass () : CVal(20) {};    // default constructor
    ChildClass (int inVal1, int inVal2: ParentClass(inVal1), CVal(inVal2) {}
    ChildClass(const ChildClass %vals: ParentClass(vals.PVal), CVal(vals.CVal) {}
    int CVal;
};

void main()
{
    ParentClass p1(4);    
    ParentClass p2 = p1;  // Copy Constructor

    p1.PVal = 2;          

    Console::WriteLine("p1.PVal=[{0}] p2.PVal=[{1}]", p1.PVal, p2.PVal);



   
  
Related examples in the same category
1.public constructor
2.Constructor inheritance
3.Constructor order
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.