0% found this document useful (0 votes)
19 views

16# Constructors Object Oriented Programming

The document contains multiple C# code snippets demonstrating the use of constructors in object-oriented programming. It includes classes for 'Product' and 'Student', showcasing both default and parameterized constructors. Additionally, it features a 'Character' class that prompts user input for creating instances.

Uploaded by

Lesunter KLter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

16# Constructors Object Oriented Programming

The document contains multiple C# code snippets demonstrating the use of constructors in object-oriented programming. It includes classes for 'Product' and 'Student', showcasing both default and parameterized constructors. Additionally, it features a 'Character' class that prompts user input for creating instances.

Uploaded by

Lesunter KLter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

16# Constructors Object Oriented Programming

///////////////
//Product.cs//
//\\\\\\\\\\\\

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

namespace darklter
{
class Product
{
public Product()
{
Console.WriteLine("Product Created");
}
}
}

///////////////
//Program.cs//
//\\\\\\\\\\\\

using System;
namespace darklter
{
class Program
{
static void Main(string[] args)
{
Product p1 = new Product();
Product p2 = new Product();
Product p3 = new Product();
}
}
}

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||

///////////////
//Product.cs//
//\\\\\\\\\\\\

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

namespace darklter
{
class Product
{
public string name;
public float price;
}
}

///////////////
//Student.cs//
//\\\\\\\\\\\\\

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

namespace darklter
{
class Student
{
public string firstName;
public string lastName;
public int grade;
public char section;

public Student(string firstName, string lastName, int grade, char section)


{
this.firstName = firstName;
this.lastName = lastName;
this.grade = grade;
this.section = section;

Console.WriteLine("Student Created");
Console.WriteLine(firstName + " " + lastName);
Console.WriteLine(grade + " - " + section);
}
}
}

///////////////
//Program.cs//
//\\\\\\\\\\\\

using System;
namespace darklter
{
class Program
{
static void Main(string[] args)
{
Product p1 = new Product();
p1.name = "Milk";
p1.price = 150;

Product p2 = new Product();


p2.name = "Milk";
p2.price = 150;
}
}
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////

using System;
namespace darklter
{
class Program
{
static void Main(string[] args)
{
Student s1 = new Student("David", "Sdpt", 5, 'E');
Student s2 = new Student("Alenere", "Sdpt", 6, 'A');
}
}
}

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||

///////////////
//Product.cs//
//\\\\\\\\\\\\

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

namespace darklter
{
class Product
{
public string name;
public float price;

public Product(string name,float price)


{
this.name = name;
this.price = price;
}
}
}

///////////////
//Program.cs//
//\\\\\\\\\\\\

using System;
namespace darklter
{
class Program
{
static void Main(string[] args)
{
Product p1 = new Product("Mik",150);
Product p2 = new Product("Coke",75);
Product p3 = new Product("Sprite", 75);
Product p4 = new Product("Royal", 75);

Console.WriteLine("Product Name : " + p2.name);


Console.WriteLine("Product Price : " + p2.price);
}
}
}

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||

using System;
namespace darklter
{
class Program
{
static void Main(string[] args)
{
Console.Write("Name : ");
string n = Console.ReadLine();

Console.Write("Voiceline : ");
string vl = Console.ReadLine();

Console.WriteLine();

Character c = new Character(n, vl);


Character c1 = new Character("lester Pogi","Tang Ina Mo Rin");
}
}

class Character
{
public string name;
public string voiceLine;

public Character(string name,string voiceLine)


{
this.name = name;
this.voiceLine = voiceLine;

Console.WriteLine(name + " : " + voiceLine);


}
}
}

You might also like