Overload constructor for different data format : constructor « Class « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » Class » constructor 
9.2.13.Overload constructor for different data format
#include<iostream.h>
#include<stdio.h>

class date
{
  int day,month,year;
public:
  date(char *str);
  date(int m,int d,int y);
  date::date();
  void show();
};
date::date(char *str)
{
  scanf(str,"*c*cd",&month,&day,&year);
}
date::date(int m,int d,int y)
{
  day=d;
  month=m;
  year=y;
}
date::date()
{
  cout<<"Enter month_day_year:";
  cin>>day;
  cin>>month;
  cin>>year;
}
void date::show()
{
  cout<<month<<'/'<<day<<'/';
  cout<<year<<'\n';
}
main()
{
  date sdate("11/1/1999");
  date idate(12,2,1998);
  date indate;
  sdate.show();
  idate.show();
  indate.show();
  return 0;
}
1
Enter month_day_year:2
3
1
16/2009312941/0
12/2/1998
3/2/1
9.2.constructor
9.2.1.A parameterized constructor
9.2.2.Use constructor to initialize class fields
9.2.3.Initialize variables and conduct calculation in constructor
9.2.4.Overload the constructor
9.2.5.Copy constructors
9.2.6.Constructor as conversion operator
9.2.7.Virtual copy constructor
9.2.8.overload constructor
9.2.9.Overload constructor two ways: with initializer and without initializer
9.2.10.Call class constructor or not during class array declaration
9.2.11.Call default constructor when allocating an array dynamically
9.2.12.Call constructor from base class to initialize fields inherited from base class
9.2.13.Overload constructor for different data format
9.2.14.Defining and using a default class constructor
9.2.15.Constructor parameter with default value
9.2.16.If a constructor only has one parameter
9.2.17.The default constructor for class X is one that takes no arguments;
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.