make an class “employee” with the atributes “name” and “salary”

paul.catalin_

New member
Joined
Jul 6, 2019
Messages
1
Programming Experience
Beginner
can someone show me some examples with this class of constructors, methods and overload ??


define 2 employee and for the first one to rais the salary, then display the employees.



i`m very new to programming, i want to learn c#
and i want to know if i started right
```
C#:
using System;

namespace ConsoleApp1
{
    class employee
    {
        public string name{ get; set; }
        private double Salary;
        
        public double salary
        {
            get { return Salary; }
            set
            {
                Salary= value;
                if (Salary< 0) Salary= 0;
            }
        }
        
        // constructor de clasa
        public employee(string _name= "Neinitializat", double _salary= 0)
        {         
            salary= _salary;
            name = _name;
        }
        
        //constructor de copiere
        public produs (salary s)
        {
            salary= s.salary;
            nsme = s.name;
        }

        public void citire()
        {
            Console.WriteLine("NAme:"); name = Console.ReadLine();
            Console.WriteLine("Salary:");Salary=double.Parse(Console.ReadLine());
        }
        
        public void scriere()
        {
            Console.WriteLine(name + ","  + Salary);
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

```
 
Last edited by a moderator:
Pretty close.

This is not a constructor, though:
C#:
//constructor de copiere
public produs (salary s)
{
    salary= s.salary;
    name = s.name;
}

A constructor would have the same name as the class. Since your class name is employee, produs above should be employee. Furthermore, it doesn't look like you have a type named salary, yet in that code above you are passing in s as some kind of salary type.

A few things about C# style:
In C#, the naming convention is to use PascalCase for classes, properties and methods; and camelCase for variables, fields, and arguments. In general, leading underscores for variable names are not recommended, but a lot of C++ to C# converts tend to name fields as _value, when in C++ they would have named their class members as m_value.

Since you didn't use code tags to wrap your code when you posted, i can't comment on your indenting, but based on the placement of the curly braces, I am guessing that you are following a good style.
 
Back
Top Bottom