Tuesday, May 1, 2012

First Class Diagrams

Types of Classes Domain – classes for the basic business model Display—presentation UI Entity Classes – handle the data (files, database access) Control classes Utility classes Border Classes Relations among classes Inheritance--generalization specialization Association—talk to each other Composition—part to whole (where if you destroy the whole you destroy the part) Aggregation—whole part where the part has its own existence Here is the class diagram

Below is the code for the inheritance example Person class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InheritanceExample
{
    class Person
    {
        private string name;

        public Person(string name)
        {
            Name = name;
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private int number;

        public int Number
        {
            get { return number; }
            set { number = value; }
        }

        public override string ToString()
        {
            return Number.ToString() + ", " + Name;
        }
    }
}


Employee Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InheritanceExample
{
    class Employee : Person
    {

        
        public Employee(string name): base(name) 
        {
            
        }
        private DateTime hiredate;

        public DateTime Hiredate
        {
            get { return hiredate; }
            set { hiredate = value; }
        }
        private string phoneNumber;

        public string PhoneNumber
        {
            get { return phoneNumber; }
            set { phoneNumber = value; }
        }

        public override string ToString()
        {
            string name= base.ToString();
            return name + ", " + PhoneNumber + ", " + Hiredate;
        }

    }
}


Salaried Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InheritanceExample
{
    class Salaried : Employee
    {
        double salary;

        public double Salary
        {
            get { return salary; }
            set { salary = value; }
        }

        public override string ToString()
        {
            string emp= base.ToString();
            return emp + "," + Salary.ToString();
        }
    }
}

No comments:

Post a Comment