Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Can you write a Template Class From your preferred programming language To be a Reference for you and for every one ?

<p><strong>Why To make that ? To help any one to switch between programming languages very fast  </strong></p> <p> </p> <p><strong>plz write the template with the following roles </strong></p> <p> </p> <p><strong>//1- class name will be Class_Name and  if it is extended it will be extended from Parent_Class _Name</strong></p> <p> </p> <p><strong>class template </strong></p> <p><strong>{</strong></p> <p><strong>//2- Visibility Scope (e.g public , protected , private etc )  with variables type (if there is )  and variable name that will be Variable_Name1 , Variable_Name2  or Object_Variable_Name1 , Object_Variable_Name2 etc   </strong></p> <p><strong> </strong></p> <p><strong>//3- construction and destruction function and Multiple constructors  with different parameters if the programming language allow that and what to do if the language don't allow that </strong></p> <p><strong> </strong></p> <p><strong> </strong></p> <p><strong>//4- Visibility Scope with function with name Function_Name() that contain if else , for , while and do while statements</strong></p> <p> </p> <p><strong> //5 - getter and setter methods for protected properties of classes}</strong></p>

user-image
Question added by Deleted user
Date Posted: 2014/11/28
Emad Mohammed said abdalla
by Emad Mohammed said abdalla , ERP & IT Software, operation general manager . , AL DOHA Company

Classes, essentially, define new types to be used in C++ code. And types in C++ not only interact with code by means of constructions and assignments. They also interact by means of operators. For example, take the following operation on fundamental types: 12 int a, b, c; a = b + c;   Here, different variables of a fundamental type (int) are applied the addition operator, and then the assignment operator. For a fundamental arithmetic type, the meaning of such operations is generally obvious and unambiguous, but it may not be so for certain class types. For example: 12345 struct myclass { string product; float price; } a, b, c; a = b + c;   Here, it is not obvious what the result of the addition operation on b and c does. In fact, this code alone would cause a compilation error, since the type myclass has no defined behavior for additions. However, C++ allows most operators to be overloaded so that their behavior can be defined for just about any type, including classes. Here is a list of all the operators that can be overloaded:Overloadable operators + - * / = < > += -= *= /= << >> <<= >>= == != <= >= ++ -- % & ^ ! | ~ &= ^= |= && || %= [] () , ->* -> new delete new[] delete[] Operators are overloaded by means of operator functions, which are regular functions with special names: their name begins by the operator keyword followed by the operator sign that is overloaded. The syntax is:type operator sign (parameters) { /*... body ...*/ }For example, cartesian vectors are sets of two coordinates: x and y. The addition operation of two cartesian vectors is defined as the addition both x coordinates together, and both y coordinates together. For example, adding the cartesian vectors (3,1) and (1,2) together would result in (3+1,1+2) = (4,3). This could be implemented in C++ with the following code: 123456789101112131415161718192021222324252627 // overloading operators example #include <iostream> using namespace std; class CVector { public: int x,y; CVector () {}; CVector (int a,int b) : x(a), y(b) {} CVector operator + (const CVector&); }; CVector CVector::operator+ (const CVector& param) { CVector temp; temp.x = x + param.x; temp.y = y + param.y; return temp; } int main () { CVector foo (3,1); CVector bar (1,2); CVector result; result = foo + bar; cout << result.x << ',' << result.y << '\\n'; return0; } 4,3 Edit & Run If confused about so many appearances of CVector, consider that some of them refer to the class name (i.e., the type) CVector and some others are functions with that name (i.e., constructors, which must have the same name as the class). For example: 12 CVector (int, int) : x(a), y(b) {} // function name CVector (constructor) CVector operator+ (const CVector&); // function that returns a CVector   The function operator+ of class CVector overloads the addition operator (+) for that type. Once declared, this function can be called either implicitly using the operator, or explicitly using its functional name: 12 c = a + b; c = a.operator+ (b);   Both expressions are equivalent.In an earlier chapter, the copy assignment function was introduced as one of the special member functions that are implicitly defined, even when not explicitly declared in the class. The behavior of this function by default is to copy the whole content of the data members of the object passed as argument (the one at the right side of the sign) to the one at the left side: 123 CVector d (2,3); CVector e; e = d; // copy assignment operator   The copy assignment member is the only operator implicitly defined for all classes. Of course, it can be redefined to any other functionality, such as, for example, to copy only certain members or perform additional initialization operations.The operator overloads are just regular functions which can have any behavior; there is actually no requirement that the operation performed by that overload bears a relation to the mathematical or usual meaning of the operator, although it is strongly recommended. For example, a class that overloads operator+ to actually subtract or that overloads operator== to fill the object with zeros, is perfectly valid, although using such a class could be challenging.The parameter expected for a member function overload for operations such as operator+ is naturally the operand to the right hand side of the operator. This is common to all binary operators (those with an operand to its left and one operand to its right). But operators can come in diverse forms. Here you have a table with a summary of the parameters needed for each of the different operators than can be overloaded (please, replace @ by the operator in each case):ExpressionOperatorMember functionNon-member function@a + - * & ! ~ ++ -- A::operator@() operator@(A) a@ ++ -- A::operator@(int) operator@(A,int) a@b + - * / % ^ & | < > == != <= >= << >> && || , A::operator@(B) operator@(A,B) a@b = += -= *= /= %= ^= &= |= <<= >>= [] A::operator@(B) - a(b,c...) () A::operator()(B,C...) - a->b -> A::operator->() - (TYPE) a TYPE A::operator TYPE() - Where a is an object of class A, b is an object of class B and c is an object of class C. TYPE is just any type (that operators overloads the conversion to type TYPE).Notice that some operators may be overloaded in two forms: either as a member function or as a non-member function: The first case has been used in the example above for operator+. But some operators can also be overloaded as non-member functions; In this case, the operator function takes an object of the proper class as first argument.For example: 123456789101112131415161718192021222324252627 // non-member operator overloads #include <iostream> using namespace std; class CVector { public: int x,y; CVector () {} CVector (int a, int b) : x(a), y(b) {} }; CVector operator+ (const CVector& lhs, const CVector& rhs) { CVector temp; temp.x = lhs.x + rhs.x; temp.y = lhs.y + rhs.y; return temp; } int main () { CVector foo (3,1); CVector bar (1,2); CVector result; result = foo + bar; cout << result.x << ',' << result.y << '\\n'; return0; } 4,3 Edit & Run

 

The keyword this

The keyword this represents a pointer to the object whose member function is being executed. It is used within a class's member function to refer to the object itself.One of its uses can be to check if a parameter passed to a member function is the object itself. For example: 12345678910111213141516171819202122 // example on this #include <iostream> using namespace std; class Dummy { public: bool isitme (Dummy& param); }; bool Dummy::isitme (Dummy& param) { if (&param == this) return true; else return false; } int main () { Dummy a; Dummy* b = &a; if ( b->isitme(a) ) cout << "yes, &a is b\\n"; return0; } yes, &a is b Edit & Run It is also frequently used in operator= member functions that return objects by reference. Following with the examples on cartesian vector seen before, its operator= function could have been defined as: 123456 CVector& CVector::operator= (const CVector& param) { x=param.x; y=param.y; return *this; }   In fact, this function is very similar to the code that the compiler generates implicitly for this class for operator=.

 

Static members

A class can contain static members, either data or functions.A static data member of a class is also known as a "class variable", because there is only one common variable for all the objects of that same class, sharing the same value: i.e., its value is not different from one object of this class to another.For example, it may be used for a variable within a class that can contain a counter with the number of objects of that class that are currently allocated, as in the following example: 12345678910111213141516171819202122 // static members in classes #include <iostream> using namespace std; class Dummy { public: static int n; Dummy () { n++; }; ~Dummy () { n--; }; }; int Dummy::n=0; int main () { Dummy a; Dummy b[5]; Dummy * c = new Dummy; cout << a.n << '\\n'; delete c; cout << Dummy::n << '\\n'; return0; } 76 Edit & Run In fact, static members have the same properties as non-member variables but they enjoy class scope. For that reason, and to avoid them to be declared several times, they cannot be initialized directly in the class, but need to be initialized somewhere outside it. As in the previous example:   int Dummy::n=0;   Because it is a common variable value for all the objects of the same class, it can be referred to as a member of any object of that class or even directly by the class name (of course this is only valid for static members): 12 cout << a.n; cout << Dummy::n;   These two calls above are referring to the same variable: the static variable n within class Dummy shared by all objects of this class.Again, it is just like a non-member variable, but with a name that requires to be accessed like a member of a class (or an object).Classes can also have static member functions. These represent the same: members of a class that are common to all object of that class, acting exactly as non-member functions but being accessed like members of the class. Because they are like non-member functions, they cannot access non-static members of the class (neither member variables nor member functions). They neither can use the keyword this.

 

Const member functions

When an object of a class is qualified as a const object:   const MyClass myobject;   The access to its data members from outside the class is restricted to read-only, as if all its data members were const for those accessing them from outside the class. Note though, that the constructor is still called and is allowed to initialize and modify these data members: 1234567891011121314151617 // constructor on const object #include <iostream> using namespace std; class MyClass { public: int x; MyClass(int val) : x(val) {} int get() {return x;} }; int main() { const MyClass foo(10); // foo.x =20; // not valid: x cannot be modified cout << foo.x << '\\n'; // ok: data member x can be read return0; } 10 Edit & Run The member functions of a const object can only be called if they are themselves specified as const members; in the example above, member get (which is not specified as const) cannot be called from foo. To specify that a member is a const member, the const keyword shall follow the function prototype, after the closing parenthesis for its parameters:   int get() const {return x;}   Note that const can be used to qualify the type returned by a member function. This const is not the same as the one which specifies a member as const. Both are independent and are located at different places in the function prototype: 123 int get() const {return x;} // const member function const int& get() {return x;} // member function returning a const& const int& get() const {return x;} // const member function returning a const&   Member functions specified to be const cannot modify non-static data members nor call other non-const member functions. In essence, const members shall not modify the state of an object.const objects are limited to access only members marked as const, but non-const objects are not restricted and thus can access both const and non-const members alike.You may think that anyway you are seldom going to declare const objects, and thus marking all members that don't modify the object as const is not worth the effort, but const objects are actually very common. Most functions taking classes as parameters actually take them by const reference, and thus, these functions can only access their const members: 123456789101112131415161718192021 // const objects #include <iostream> using namespace std; class MyClass { int x; public: MyClass(int val) : x(val) {} const int& get() const {return x;} }; void print (const MyClass& arg) { cout << arg.get() << '\\n'; } int main() { MyClass foo (10); print(foo); return0; } 10 Edit & Run If in this example, get was not specified as a const member, the call to arg.get() in the print function would not be possible, because const objects only have access to const member functions.Member functions can be overloaded on their constness: i.e., a class may have two member functions with identical signatures except that one is const and the other is not: in this case, the const version is called only when the object is itself const, and the non-const version is called when the object is itself non-const. 12345678910111213141516171819202122 // overloading members on constness #include <iostream> using namespace std; class MyClass { int x; public: MyClass(int val) : x(val) {} const int& get() const {return x;} int& get() {return x;} }; int main() { MyClass foo (10); const MyClass bar (20); foo.get() =15; // ok: get() returns int& // bar.get() =25; // not valid: get() returns const int& cout << foo.get() << '\\n'; cout << bar.get() << '\\n'; return0; } 1520 Edit & Run

 

Class templates

Just like we can create function templates, we can also create class templates, allowing classes to have members that use template parameters as types. For example: 123456789 template <class T> class mypair { T values [2]; public: mypair (T first, T second) { values[0]=first; values[1]=second; } };   The class that we have just defined serves to store two elements of any valid type. For example, if we wanted to declare an object of this class to store two integer values of type int with the values115 and36 we would write:   mypair<int> myobject (115,36);   This same class could also be used to create an object to store any other type, such as:   mypair<double> myfloats (3.0,2.18);   The constructor is the only member function in the previous class template and it has been defined inline within the class definition itself. In case that a member function is defined outside the defintion of the class template, it shall be preceded with the template <...> prefix: 1234567891011121314151617181920212223242526 // class templates #include <iostream> using namespace std; template <class T> class mypair { T a, b; public: mypair (T first, T second) {a=first; b=second;} T getmax (); }; template <class T> T mypair<T>::getmax () { T retval; retval = a>b? a : b; return retval; } int main () { mypair <int> myobject (100,75); cout << myobject.getmax(); return0; } 100 Edit & Run Notice the syntax of the definition of member function getmax: 12 template <class T> T mypair<T>::getmax ()   Confused by so many T's? There are three T's in this declaration: The first one is the template parameter. The second T refers to the type returned by the function. And the third T (the one between angle brackets) is also a requirement: It specifies that this function's template parameter is also the class template parameter.

 

Template specialization

It is possible to define a different implementation for a template when a specific type is passed as template argument. This is called a template specialization.For example, let's suppose that we have a very simple class called mycontainer that can store one element of any type and that has just one member function called increase, which increases its value. But we find that when it stores an element of type char it would be more convenient to have a completely different implementation with a function member uppercase, so we decide to declare a class template specialization for that type: 12345678910111213141516171819202122232425262728293031323334 // template specialization #include <iostream> using namespace std; // class template: template <class T> class mycontainer { T element; public: mycontainer (T arg) {element=arg;} T increase () {return ++element;} }; // class template specialization: template <> class mycontainer <char> { char element; public: mycontainer (char arg) {element=arg;} char uppercase () { if ((element>='a')&&(element<='z')) element+='A'-'a'; return element; } }; int main () { mycontainer<int> myint (7); mycontainer<char> mychar ('j'); cout << myint.increase() << endl; cout << mychar.uppercase() << endl; return0; } 8 J Edit & Run This is the syntax used for the class template specialization:   template <> class mycontainer <char> { ... };   First of all, notice that we precede the class name with template<> , including an empty parameter list. This is because all types are known and no template arguments are required for this specialization, but still, it is the specialization of a class template, and thus it requires to be noted as such.But more important than this prefix, is the <char> specialization parameter after the class template name. This specialization parameter itself identifies the type for which the template class is being specialized (char). Notice the differences between the generic class template and the specialization: 12 template <class T> class mycontainer { ... }; template <> class mycontainer <char> { ... };   The first line is the generic template, and the second one is the specialization.When we declare specializations for a template class, we must also define all its members, even those identical to the generic template class, because there is no "inheritance" of members from the generic template to the specialization.

Python and java are best two programming languages

package aslam.com;

class Program

{

public static void main(String[] args)

{

System.out.println("Java is the most secured and platform independent programming Language");

}

}

// This is the programming sample of Java

---------------------------------------------------------------------

<!----This is format of Web Desing

<!Doctype html>

<html>

<head>

<title>Web Page </title>

</head>

<body>

</body>

</html>

</head.

 

Prof Ashraf Anwar
by Prof Ashraf Anwar , Head of MIS DEPT and Associate Professor , High Institute for MIS and ENG

template <class T>

T add(T v1, T v2){  // Generic Type T

    T sum = v1+v2; // Sum any Values of Generic Type T

    return sum;

}

omg i can't this is a bit difficult :/

mohamed el haddad
by mohamed el haddad , wordpress developer , bayantech

<!doctype html>

<html>

<head>

<title>My First Webpage</title>

!

<meta charset="utf-8" />

<meta http-equiv="Content-type" content="text/html; charset=utf-8" />

<meta name="viewport" content="width=device-width, initial-scale=1" />

!

<!-- Latest compiled and minified CSS -->

<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/

bootstrap.min.css">

!

<!-- Optional theme -->

<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/

bootstrap-theme.min.css">

!

!

<style>

!

html, body {

height:100%;

}

!

.container {

background-image:url("background.jpg");

width:100%;

height:100%;

background-size:cover;

background-position:center;

padding-top:100px;

}

!

.center {

text-align:center;

}

!

.white {

color:white;

}

!

p {

padding-top:15px;

padding-bottom:15px;

}

!

button {

margin-top:20px;

margin-bottom:20px;

}

!

.alert {

margin-top:20px;

display:none;

}

</style>

!

!

</head>

!

<body>

!

<div class="container">

!

<div class="row">

!

<div class="col-md-6 col-md-offset-3 center">

!

<h1 class="center white">Weather Predictor</h1>

!

<p class="lead center white">Enter your city below to get a forecast for the

weather.</p>

!

<form>

!

<div class="form-group">

!

<input type="text" class="form-control" name="city" id="city" placeholder="Eg.

London, Paris, San Francisco..." />

!

</div>

!

<button id="findMyWeather" class="btn btn-success btn-lg">Find My Weather</

button>

!

!

</form>

!

!

<div id="success" class="alert alert-success">Success!</div>

!

<div id="fail" class="alert alert-danger">Could not find weather data for that

city. Please try again.</div>

!

<div id="noCity" class="alert alert-danger">Please enter a city!</div>

!

</div>

!

!

!

!

!

</div>

!

!

</div>

!

!

!

!

!

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>

!

!

<!-- Latest compiled and minified JavaScript -->

<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></

script>

!

<script>

!

$("#findMyWeather").click(function(event) {

event.preventDefault();

$(".alert").hide();

if ($("#city").val()!="") {

$.get("scraper.php?city="+$("#city").val(),

function( data ) {

if (data=="") {

$("#fail").fadeIn();

} else {

$("#success").html(data).fadeIn();

}

});

} else {

$("#noCity").fadeIn();

}

});

!

!

</script>

!

</body>

</html>

Jose Manuel Mota
by Jose Manuel Mota , Employee , Guardianes del Sur

Yes, I can do it. it depends of the algorithm 

Abhishek Chatterjee
by Abhishek Chatterjee , Technical Specialist , Philips Innovation

 

 The is example class template in Swift

import UIKit

 

 

 

 

class SampleVC: UIViewController, UITableViewDataSource, UITableViewDelegate

 

 

{

 

 

//MARK: IBOutlets

 

 

@IBOutlet weak var nameBtn: UIButton!

 

 

@IBOutlet weak var anotherBtn: UIButton!

 

   

 

 

//MARK: CONSTANTS

 

 

//private and fileprivate constants

 

 

private let someConstant: Int = 0

 

 

private let anotherConstant: String = "Hello"

 

   

 

 

//public constants

 

 

let somePublicConstant: Int = 0

 

 

let someOtherConstant: String = "Hello again"

 

   

 

 

//MARK: VARIABLES

 

 

//private and fileprivate vars

 

 

private var someVariable: Int?

 

 

private var anotherVariable: String?

 

   

 

 

//public constants

 

 

var somePublicVariable: Int?

 

 

var anotherPublicVariable: String?

 

   

 

   

 

 

//MARK: INIT and DEINIT methods

 

 

convenience init()

 

 

{

 

 

self.init()

 

 

}

 

   

 

 

required init?(coder aDecoder: NSCoder)

 

 

{

 

 

       fatalError("init(coder:) has not been implemented")

 

 

}

 

   

 

 

deinit

 

 

{

 

   

 

 

}

 

   

 

 

//MARK: OVERRIDE METHODS

 

 

override func viewDidLoad()

 

 

{

 

 

       super.viewDidLoad()

 

 

}

 

 

 

 

 

override func didReceiveMemoryWarning()

 

 

{

 

 

super.didReceiveMemoryWarning()

 

 

//Dispose of any resources that can be recreated.

 

 

}

 

   

 

 

//MARK: PRIVATE METHODS

 

 

private func somePrivateMethod()

 

 

{

 

   

 

 

}

 

   

 

 

//MARK: PUBLIC METHODS

 

 

func somePublicMethod()

 

 

{

 

   

 

 

}

 

   

 

 

//MARK: DELEGATE METHODS

 

 

//MARK: UITableViewDelegate

 

 

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat

 

 

{

 

 

return 100

 

 

}

 

   

 

 

//MARK: UITableViewDataSource

 

 

func numberOfSections(in tableView: UITableView) -> Int

 

 

{

 

 

return 1

 

 

}

 

   

 

 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

 

 

{

 

 

return 5

 

 

}

 

   

 

 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

 

 

{

 

 

let cell: BaseTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! BaseTableViewCell

 

   

 

 

return cell

 

 

}

 

 

}

 

         

 

 

 

Abdul Khader Shaik
by Abdul Khader Shaik , IT & Ops Manager , TheSmartlabs

When you know how to create templates and their properties..why are you posting/asking ..?

lawrence modisane
by lawrence modisane , Information Technology Specialist , matrix warehouse

visual basic most recent version

Deleted user
by Deleted user

Basm Allah Alrahman Alrahim

 

<?php/

 

/1- class name will be Class_Name and if it is extended it will be extended from Parent_Class _Name

 

class template

{

 

//2- Visibility Scope (e.g public , protected , private etc ) with variables type (if there is )and variable name that will be Variable_Name1 , Variable_Name2 or Objec_Variable_Name1 , Object_Variable_Name2 etc  

 

public Static $Variable_Name1;    

private $Variable_Name2= false;

// DIFFERENT ways for array declaration

//A    

public $Variable_Name3 [''] = '';     

//B    

public $Variable_Name4 = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");    

//C another way of B    

public $Variable_Name5['Peter'] = "35";    

public $Variable_Name5['Ben'] = "37";    

public $Variable_Name5['Joe'] = "43";     

//D multidimension array or array of arrays    

public $Variable_Name6 = array      (          array("Peter",22,18),          array("Ben",15,13),          array("Joe",5,2),          array("Mors",1,5)      );

 

//3- construction and destruction function and Multiple constructors with different parameters if the programming languge allow that and what to do if the language don't allow that

 

     public function __construct() // or public function template() { }

    {

        $this->$Variable_Name2=true; // obsrve $this mean this object from this class

    }

 

//4- Visibility Scope with function with name Function_Name() that contain if else , for , while and do while statements

    public function Function_Name( $parameter_Name_1 , $parameter_Name_2 , $parameter_Name_n)

    {

        //4 -1 if else statment

        if (condition)

        {

            //if condition is true execute codes;

        }

        elseif (condition)

        {

            //if condition is true execute codes;

        }

        else

        {

            //if condition is false execute codes;

        }

        //4 -2 For loop statment

        for (inititial counter Variable ; test counter Variable; increment counter Variable)

        {

            //looping code to be executed;

        }

        //4 -3 while loop statment

        while (condition is true)

        {

            //looping code to be executed;

        }

        //4 -4 do while loop statment

        do {

            //looping code to be executed;

        } while (condition is true);

        //4 -5 foreach loop statment

        foreach ($array as $value)

        {

            //looping code to be executed;

        }

        //4 -6 swith statment is just simple way from if else statments

        switch (n)

        {

            case label1:

                //code to be executed if n=label1;

            break;

                        case label2:

                //code to be executed if n=label2;

            break;

                case label3:

                //code to be executed if n=label3;

            break;

            ... 

           default:

                //code to be executed if n is different from all labels;

        }

 

        //5 - getter and setter methods for protected properties of classes 

 

       // $this->Variable_Name , $this->Function_Name

    }}

 

$VarObjectFromMytemplateClass =new template();

 

required_once('template_2_file.php');

 

//observe :: operators used with static functionns

 

template_2::Static_Function_1();

template_2::Static_Function_2();

 

//observe $Class_Name->Variable_Name (i.e -> look like dot in programming )?>

 

?>

More Questions Like This