0% found this document useful (0 votes)
28 views29 pages

Lab Oop End Code

The document contains code examples demonstrating class creation and usage in C++ and C#. It covers various topics such as access modifiers, static members, constructors, destructors, properties, and inheritance. Each section includes code snippets and explanations for better understanding of object-oriented programming concepts.

Uploaded by

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

Lab Oop End Code

The document contains code examples demonstrating class creation and usage in C++ and C#. It covers various topics such as access modifiers, static members, constructors, destructors, properties, and inheritance. Each section includes code snippets and explanations for better understanding of object-oriented programming concepts.

Uploaded by

hafsamohee1999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Create class in c++

#include <iostream>
#include <string>
using namespace std;
int c;
struct stperson
{
string FirstName;
string LastNAme;
string FullName()
{
return FirstName + " " + LastNAme;
}
};
class clsperson {
//private:
int x = 0;
public:
string FirstName;
string LastNAme;

string FullName()
{
return FirstName + " " + LastNAme;
}
};

int main()
{
stperson Person1;
[Link] = "Ali";
clsperson Person;//object

[Link] = "Anhar";
[Link] = "Ali";

cout << [Link]()<<endl;

string s1;

1 ‫الصفحة‬
Create class in c#
using System;
namespace test1
{
class clsPerson
{

//Fields
public string FirstName;
public string LastName;

//Method
public string FullName()
{
return FirstName + ' ' + LastName;
}
}

class Program
{
static void Main(string[] args)
{
//Create object from class
clsPerson Person1 = new clsPerson();

[Link]("Accesseing Object 1 (Person1):");


[Link] = "Anhar";
[Link] = "Ali";
[Link]([Link]());

//Create ANOTHER object from class


clsPerson Person2 = new clsPerson();

[Link]("Accesseing Object 2 (Person2):");


[Link] = "Ali";
[Link] = "Ali";
[Link]([Link]());

[Link]();

}
}
}

2 ‫الصفحة‬
Access Modifiers in c#
using System;
namespace AccessModifiers
{
class clsA
{
public int x1 = 10;
private int x2 = 20;
protected int x3 = 30;
public int fun1() { return 100; }
private int fun2() { return 200; }
protected int fun3() { return 300; }
}

class clsB : clsA


{
public int fun4() { return x1 + x3 ; }
}
class Program
{
static void Main(string[] args)
{
clsA A = new clsA();

[Link]("all public members are accessable");


[Link]("x1={0}", A.x1);

//[Link]("x2={0}", A.x2);
//[Link]("x3={0}", A.x3);

[Link]("result of fun1{0}", A.fun1());


//[Link]("result of fun2{0}", A.fun2());
//[Link]("result of fun3{0}", A.fun3());

clsB B = new clsB();


[Link]("\nObjects from class B expose all public memebers from class
A");

[Link]("x1={0}", B.x1);
// [Link]("x2={0}", B.x2);
// [Link]("x3={0}", B.x3);

[Link]("result of fun1{0}", B.fun1());


[Link]("result of fun4{0}", B.fun4());
// [Link]("result of fun2{0}", B.fun2());
// [Link]("result of fun3{0}", B.fun3());

[Link]();

}
}
}

3 ‫الصفحة‬
‫الصفحة ‪4‬‬
Static Member in c#
using System;
namespace StaticMember
{
class clsA
{
public int x1;
//x2 is shared for all object because it is on the class level
public static int x2;
public int Method1()
{//not static methods can always access the static members

return x1 + x2 +Method2();
//return x1 + x2 ;
}
public static int Method2()
{ //static methods cannot access non-staic member because there is no object
//static methods are called at the class level.
//x1 public level object no level class
//static update static member only
//return clsA. x2;
//return x1;
return x2;
}
}
internal class Program
{
static void Main(string[] args)
{ //create an object of clsA class.
clsA objA1 = new clsA();
clsA objA2 = new clsA();
objA1.x1 = 7;
objA2.x1 = 10;
//‫ ماتسمح لك بالتعديل اال عن طريق اسم الكالس وليس عن طريق‬object
//x2 is shared for all object because it is on the class level, you can access
//using the class name
clsA.x2 = 100;
//objA1.x2 = 100;

[Link]("objA1.x1={0}", objA1.x1);// 7
[Link]("objA2.x1={0}", objA2.x1);// 10
// [Link]("objA1.method1 result={0}", clsA.Method1());
[Link]("objA1.method1 result={0}", objA1.Method1());
[Link]("objA2.method1 result={0}", objA2.Method1());
//method 2 cannot be access through object , only through the class itself.
//[Link](objA1.Method2());
[Link]("static method2 result={0}", clsA.Method2());

[Link]("static x2={0}", clsA.x2);

[Link]();
}
}
}

5 ‫الصفحة‬
Get_and_set_E1 in c#
using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace Get_and_set_E1
{
class clsEmployee
{ // Private fields
private int _ID;
private string _Name = [Link];
//ID Property Declaration
public int ID
{ //Get is use for Reading field
get { return _ID; }

//Set is use for writing field


// set { _ID = value; }
set { this._ID = value; }
}
//Name Property Declaration

public string Name


{//Get is use for Reading field
get { return _Name; }
//Set is use for writing field
set { _Name = value; }
}
}
internal class Program
{

static void Main(string[] args)


{
clsEmployee Employee1 = new clsEmployee();
[Link]= 1;
[Link] = "Anhar Ali";

[Link]("Employee ID ={0}", [Link]);


[Link]("Employee Name ={0}", [Link]);

[Link]();
}
}
}

6 ‫الصفحة‬
Read Only Properties in c#
using System;
namespace ReadOnlyProperties
{
class clsEmployee
{ // Private fields
private int _ID;
private string _Name = [Link];
//ID Property Declaration as readonly
public int ID
{
//Get is use for Reading field
get { return _ID; }
}
//Name Property Declaration
public string Name
{
//Get is use for Reading field

get { return _Name; }

//Set is use for writing field in _name save old value in file orDB &&value save
new value

set { _Name = value; }


}
}

internal class Program


{
static void Main(string[] args)
{ //Create an object of Employee class.
clsEmployee Employee1 = new clsEmployee();
// You cannot modify the id value because it's read only
// [Link] = 7;

[Link] = "Anhar Ali";

[Link]("Employee ID ={0}", [Link]);


[Link]("Employee Name ={0}", [Link]);

[Link]();
}
}
}

7 ‫الصفحة‬
Auto Implemented Property in c#
using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace AutoImplementedProperty
{
class clsEmployee
{//ID property
public int ID { get; set; }
//Name property
public string Name { get; set; }

}
class Program
{
static void Main(string[] args)
{
clsEmployee Employee1 = new clsEmployee();
[Link] = 1;
[Link] = "Anhar";
[Link]("Employess id :{0} Employess Name:{1}",
[Link],[Link]);
[Link]();

}
}
}

8 ‫الصفحة‬
Static Property AND Static Class in c#
using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace StaticPropertyANDStaticClass
{static class clsSetting
{
public static int DayNumber
{
get { return [Link]; }

}
public static string DayName
{
get { return [Link](); }
}
public static string ProjectPath { get; set; }

}
class Program
{
static void Main(string[] args)
{
//clsSetting set = new clsSetting();
//Read the static Properties
[Link]([Link]);
[Link]([Link]);
//change the value the static bool property
[Link]=@"c:\myprojects\";
[Link]([Link]);
[Link]();

}
}
}

9 ‫الصفحة‬
Constructor
using System;

namespace parameter_less_constructor
{
class clsperson
{
public int ID { set; get; }
public string Name { set; get; }
public string Phone { set; get; }

//parameter_less_constructor
public clsperson()
{
ID = -1;
Name = "Empty";
Phone = "67778";
}
//parameter_constructor
public clsperson(int id, string name,string phone)
{
[Link] = id;
[Link] = name;
[Link] = phone;
}

//step 2
static class clssettings
{
public static int DayNumber
{
get { return [Link]; }
}
public static string Dayname
{
get { return [Link](); }
}
public static string projectPath { get; set; }
//step 1
//private clssettings()
//{//this is a private constructor to
// //prevent creating object from this class
//}
static clssettings()
{
projectPath = @"c:\myproject\S\d";
}
}
class Program
{
static void Main(string[] args)
{
//clsperson person1 = new clsperson();
//[Link]("ID := {0}", [Link]);
//[Link]("Name := {0}", [Link]);
//[Link]("Phone:= {0}", [Link]);
clsperson person2 = new clsperson(3, "Ali", "355");
[Link]("ID := {0}", [Link]);
[Link]("Name := {0}", [Link]);
[Link]("Phone:= {0}", [Link]);

10 ‫الصفحة‬
//clssettings set = new clssettings();
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);

[Link]();

}
}
}

11 ‫الصفحة‬
Destructor
using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace Destructor
{ class clsperson
{
public clsperson()
{
[Link]("Constructor called.");
}
~clsperson()
{
[Link]("Destructor called.");
}

}
class Program
{
static void Main(string[] args)
{
clsperson person1 = new clsperson();
[Link]();
}
}
}

12 ‫الصفحة‬
Application_of_using_static_constructors
using System;
namespace Application_of_using_static_constructors
{ class clsperson
{
public int ID { get; set; }
public string Name { get; set; }
public short Age { get; set; }
public string UserName { get; set; }
public string PassWord { get; set; }

public clsperson(int id , string name,short age)


{
[Link] = id;
[Link] = name;
[Link] = age;
}
public static clsperson Find(int ID)
{
if (ID == 10)
{
return new clsperson(10, "Ahmed", 22);
}
else
{
return null;
}
}
public static clsperson Find(string username,string passwoed)
{
if (username == "Anhar" && passwoed == "123")
{
return new clsperson(10, "Anhar", 22);
}
else
{
return null;
}
}

}
class Program
{
static void Main(string[] args)
{
clsperson p1 = new clsperson(1, "Ali", 30);
[Link]("\n\n Finding person 2 by id");
clsperson p2 = [Link](10);
if(p2!=null)
{
[Link]("id ={0}", [Link]);
[Link]("name ={0}", [Link]);
[Link]("age ={0}", [Link]);

}
else
{
[Link]("Couldn't find the person by the gieven id");
}

[Link]("\n\n Finding person 3 by username and password");

13 ‫الصفحة‬
clsperson p3 = [Link]("Anhar","123");
if (p3 != null)
{
[Link]("id ={0}", [Link]);
[Link]("id ={0}", [Link]);
[Link]("id ={0}", [Link]);

}
else
{
[Link]("Couldn't find the person by the gieven id");
}
[Link]();
}
}
}

14 ‫الصفحة‬
Inheritance
using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace Inheritance_0
{
public class clsperson
{
public int ID { get; set; }
public string firstName { get; set; }
public string LastName { get; set; }
public string Title { get; set; }
public string FullName
{
get
{
return firstName + ' ' + LastName;
}
}
}
public class clsEmployee : clsperson
{
public float Salary { get; set; }
public string DepartmentName { get; set; }
public void increasesalary(float Amount)
{
Salary += Amount;

}
class Program
{
static void Main(string[] args)
{ //create object of Empoyee
clsEmployee Emp1 = new clsEmployee();
[Link]("Employees Object one ");
//the following inherited from base class person
[Link] = 1;
[Link] = "Ali";
[Link] = "Ali";
[Link] = "AAA";
//the following are from derived class Employee
[Link] = "IT";
[Link] = 66;
[Link]("ID : = {0}", [Link]);
[Link]("full name: = {0}", [Link]);
[Link]("Title: = {0}", [Link]);
[Link]("Salary : = {0}", [Link]);
[Link](100);
[Link]("increase salary : = {0}", [Link]);
[Link]();
}
}
}

15 ‫الصفحة‬
Inheritance Constructor
using System;
namespace Inheritance_1
{
public class clsperson
{
public int ID { get; set; }
public string firstName { get; set; }
public string LastName { get; set; }
public string Title { get; set; }
public string FullName
{
get
{
return firstName + ' ' + LastName;
}
}

public clsperson(int id,string firstName,string lastname,string title)


{
[Link] = id;
[Link] = firstName;
[Link] = lastname;
[Link] = title;

}
public class clsEmployee:clsperson
{ public clsEmployee(int id, string firstName, string lastname, string title,
float salary,string departmentName) : base ( id, firstName, lastname, title)
{
[Link] = salary;
[Link] = departmentName;

}
public float Salary { get; set; }
public string DepartmentName { get; set; }
public void increasesalary(float Amount)
{
Salary += Amount;

}
}
class Program
{
static void Main(string[] args)
{
clsEmployee Emp2 = new clsEmployee(1, "Ali", "Ahmed", "te", 1000, "Ai");
[Link]("ID : = {0}", [Link]);
[Link]("full name: = {0}", [Link]);
[Link]("full name: = {0}", [Link]);
[Link]("Title: = {0}", [Link]);
[Link]("Salary : = {0}", [Link]);
[Link](100);
[Link]("increase salary : = {0}", [Link]);
[Link]();

}
}
}

16 ‫الصفحة‬
Multi inheritance
using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace Multi_inheritance
{
public class person
{
public string Name { get; set; }
public short Age { get; set; }
public void Introduce()
{
[Link]($" Hi , my name is{Name} and I am {Age} yeare old .");
}
}
public class Emplouee:person
{
public string EmployeeID { get; set; }
public decimal Salary { get; set; }
public void Work()
{
[Link]($"Employee with Id {EmployeeID} and salary {Salary} is working");
}

}
public class Doctor :Emplouee
{
public string Specialty { get; set; }
public void depart()
{
[Link]($"Doctor {Name}with id {EmployeeID}, salary {Salary} ,and
specialty {Specialty}");
}
}
class Program
{
static void Main(string[] args)
{
Doctor doctor = new Doctor();
[Link] = "Ahmed";
[Link] = 30;
[Link] = "AI";
[Link]();
[Link]();
[Link]();
}
}
}

17 ‫الصفحة‬
Upcasting Down casting
using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace Upcasting_Downcasting
{ public class person
{
public string Name { get; set; }
public short Age { get; set; }
public void Greet()
{
[Link]($" Hi , my name is{Name} and I am {Age} yeare old .");
}
}
public class Emloyee :person
{
public string Company { get; set; }
public decimal Salary { get; set; }
public void Work()
{
[Link]($"I work at {Company} and {Salary} real");
}

}
class Program
{
static void Main(string[] args)
{
//Upcasting
Emloyee Emp1 = new Emloyee { Name = "Ahmed", Age = 30, Company = "Yemen Net",
Salary = 3000 };
person per = Emp1;
[Link]();//output "Hi , my name is Ahmed and i am 30 yeare old."
//Downcasting
person per2 = new Emloyee { Name = "Ali", Age = 30, Company = "Yemen Net", Salary =
3000 };
Emloyee emp2 = (Emloyee)per2;
[Link]();
person per3 = new person { Name = "Omer", Age = 40 };
// Emloyee emp3 = (Emloyee)per3;
[Link]();

}
}
}

18 ‫الصفحة‬
Method Overriding
using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace Method_Overriding
{ public class clsA
{
public virtual void print()
{
[Link]("HI i am the print method from the base class A");
}
}
public class clsB :clsA
{
public override void print()
{
[Link]("Hi , I am the print method from the derived class B");
[Link]();
}
}
public class clsC : clsA
{
public override void print()
{
[Link]("Hi , I am the print method from the derived class C");
[Link]();
}
}
class Program
{
static void Main(string[] args)
{
//create an object of Empoyee
clsB objB = new clsB();
[Link]();
clsC objc = new clsC();
[Link]();
[Link]();
}
}
}

19 ‫الصفحة‬
Method Hiding shodowing
using System;
namespace Method_Hiding_shodowing
{
public class MyBaseClass
{
public virtual void MyMethod()
{
[Link]("Base class implementation");
}

public virtual void MyOtherMethod()


{
[Link]("Base class implementation of MyOtherMethod");
}
}

public class MyDerivedClass : MyBaseClass


{
public override void MyMethod()
{

[Link]("Derived class implementation using override");

public new void MyOtherMethod()


{
[Link]("Derived class implementation of MyOtherMethod using new");
}
}

class Program
{
static void Main(string[] args)
{
MyBaseClass myBaseObj = new MyBaseClass();
[Link]("\nBase Object:\n");
[Link]();
[Link]();

MyDerivedClass myDerivedObj = new MyDerivedClass();


[Link]("\nDerived Object:\n");
[Link]();
[Link]();

MyBaseClass myDerivedObjAsBase = myDerivedObj;


[Link]("\nAfter casting:\n");
[Link]();
[Link]();

[Link]();
}
}
}

20 ‫الصفحة‬
Abstract Class and Method
using System;

namespace AbstractClass
{
public abstract class Clsperson
{
public string FirstName { get; set; }
public string LastName { get; set; }
public abstract void Introduce();
public void sayGoodbye()
{
[Link]("Goodbye!");
}
}
public class clsEmployee :Clsperson
{
public int Employeeid { get; set; }
public override void Introduce()
{
[Link]($"Hi , my name is {FirstName} {LastName}{Employeeid},and my
Emloyee");
}
}

class Program
{
static void Main(string[] args)
{
// Clsperson person = new Clsperson();

clsEmployee Emp = new clsEmployee();


[Link] = "Ahmed";
[Link] = "Ali";
[Link] = 12;
[Link]();
[Link]();
[Link]();
}
}
}

21 ‫الصفحة‬
Implement Multiple InterFace
using System;

namespace ImplementMultipleInterFace
{
public interface IPerson
{
string FirstName { get; set; }
string LastName { get; set; }
void Interoduce();
void Print();

public interface ICommunicate


{
void CallPhone();
void SendEmail(string Title,string Body);
void SendSMS(string Title, string Body);
}

public abstract class ClsPerson : IPerson,ICommunicate


{
public string FirstName { get; set; }
public string LastName { get; set; }
public abstract void Interoduce();
public void sayGoodbye()
{
[Link]("Goodbye");
}
public void Print()
{
[Link]("HI I'm the print method");
}
public void CallPhone()
{
[Link]("CallPhone .");
}
public void SendEmail(string Title,string body)
{
[Link]("Email Send .");
}
public void SendSMS(string Title, string body)
{
[Link]("SMS Send .");
}

}
public class Employee : ClsPerson
{
public int EmployeeId { get; set; }

public override void Interoduce()


{
[Link]($"Hi, my name is {FirstName} {LastName}, and my employee ID is
{EmployeeId}.");
}
}
class Program
{

22 ‫الصفحة‬
static void Main(string[] args)
{
Employee employee = new Employee();
[Link] = "Ali";
[Link] = "Ahmed";
[Link] = 123;
[Link](); // Output: "Hi, my name is John Doe, and my employee ID is
123."
[Link](); // Output: "Goodbye!"
[Link]();
[Link]("Hi", "Send Email");
[Link]();
[Link]("Hi","Send SMS");

[Link]();
}
}
}

23 ‫الصفحة‬
Nested Class
usiusing System;

namespace Nested_Class
{
public class OuterClass
{
private int OuterVariable;

public OuterClass(int outerVariable)


{
[Link] = outerVariable;
}

public void OuterMethod()


{
[Link]("Outer Method called.");
}

public class InnerClass


{
private int InnerVariable;

public InnerClass(int innerVariable)


{
[Link] = innerVariable;
}

public void InnerMethod()


{
[Link]("Inner Method with inner variable = " + InnerVariable);
}

public void AccessOuterVariable(OuterClass outer)


{
[Link]("Accessing outer variable from inner class: " +
[Link]);
}
}
}

class Program
{
static void Main(string[] args)
{
OuterClass outer1 = new OuterClass(42);

[Link] inner1 = new [Link](100);

[Link]();
[Link]();
[Link](outer1);

[Link]();

}
}
}

24 ‫الصفحة‬
Composition
using System;

namespace Composition
{
class clsA
{
public int x;
public int y;

public void Method1()


{
[Link]("Method1 of class A is called");
}

public void Method2()


{
[Link]("Method2 of class A is called");
[Link]("Now i will call method1 of class B...");

//defining an object of another class inside this class is called composition.


ClsB ObjectB1 = new ClsB();
ObjectB1.Method1();
}

}
class ClsB
{
public void Method1()
{
[Link]("Method1 of class B is called");
}

}
class Program
{
static void Main(string[] args)
{

//Create object from class


clsA ObjectA1 = new clsA();
ObjectA1.Method1();
ObjectA1.Method2();

[Link]();

}
}
}

25 ‫الصفحة‬
Sealed Class Method
using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace Sealed_Class_Method
{
//sealed class clsA
//{

//}

//// trying to inherit sealed class


//// Error Code
//class clsB : clsA
//{

//}

public class Person


{
public virtual void Greet()
{
[Link]("The person says hello.");
}
}

public class Employee : Person


{
public sealed override void Greet()
{
[Link]("The employee greets you.");
}
}

public class Manager : Employee


{
//This will produce a compile-time error because the Greet method in Employee is
//sealed and cannot be overridden.
//public override void Greet()
//{
// [Link]("The manager greets you warmly.");
//}
}

class Program
{
static void Main(string[] args)
{

//// create an object of B class


//clsB B1 = new clsB();

Person person = new Person();

26 ‫الصفحة‬
[Link](); // outputs "The person says hello."

Employee employee = new Employee();


[Link](); // outputs "The employee greets you."

Manager manager = new Manager();


[Link](); // outputs "The employee greets you."

[Link]();

}
}
}

27 ‫الصفحة‬
partial class
using System;

public partial class Person


{
public int Age { get; set; }

partial void PrintAge();

public void Birthday() {

Age++;
PrintAge();
}
}

public partial class Person


{
partial void PrintAge()
{
[Link]("Current age: {0}",Age);

}
}

public class Program


{
public static void Main(string[] args)
{
Person person1=new Person();
[Link] = 25;
[Link]();
[Link]();

}
}

28 ‫الصفحة‬
MyFirst Class Library
using System;

namespace MyFirstClassLibrary
{
public class MyMath
{
public int Sum(int x, int y) { return x + y; }

public int Sum(int x, int y, int z) { return x + y + z; }


}
}

TestMyFirstClassLibraryProject
using System;
using MyFirstClassLibrary;

namespace TestMyFirstClassLibraryProject
{
internal class Program
{
static void Main(string[] args)
{
[Link]("hi");
MyMath myMath = new MyMath();

[Link]([Link](10, 10));
[Link]();
}
}
}

29 ‫الصفحة‬

You might also like